diff --git a/com/xgiovio/ArrayIndexListTest.java b/com/xgiovio/ArrayIndexListTest.java index 6af9441..6943f9d 100644 --- a/com/xgiovio/ArrayIndexListTest.java +++ b/com/xgiovio/ArrayIndexListTest.java @@ -1,10 +1,7 @@ package com.xgiovio; import arraylist.ArrayIndexList; -import general_utility.test_object; -import sequence.NodeSequence; -import sequence.Sequence; -import stack.NodeStack; +import com.xgiovio.general_utility.test_object; import java.util.Iterator; diff --git a/com/xgiovio/ArraySequenceTest.java b/com/xgiovio/ArraySequenceTest.java index a3a11d6..83a6f7b 100644 --- a/com/xgiovio/ArraySequenceTest.java +++ b/com/xgiovio/ArraySequenceTest.java @@ -1,6 +1,6 @@ package com.xgiovio; -import general_utility.test_object; +import com.xgiovio.general_utility.test_object; import position.Position; import sequence.ArraySequence; diff --git a/com/xgiovio/ArrayTest.java b/com/xgiovio/ArrayTest.java index 648b9e4..2b7e455 100644 --- a/com/xgiovio/ArrayTest.java +++ b/com/xgiovio/ArrayTest.java @@ -1,7 +1,6 @@ package com.xgiovio; -import general_utility.test_object; -import stack.ArrayStack; +import com.xgiovio.general_utility.test_object; import stack.NodeStack; /** diff --git a/com/xgiovio/DequeTest.java b/com/xgiovio/DequeTest.java index f611f36..5315eff 100644 --- a/com/xgiovio/DequeTest.java +++ b/com/xgiovio/DequeTest.java @@ -1,9 +1,7 @@ package com.xgiovio; -import deque.Deque; import deque.NodeDeque; -import general_utility.test_object; -import stack.NodeStack; +import com.xgiovio.general_utility.test_object; /** * Created with xgiovio.macbookair. diff --git a/com/xgiovio/HashDictionaryChainingTest.java b/com/xgiovio/HashDictionaryChainingTest.java new file mode 100644 index 0000000..4d14ce9 --- /dev/null +++ b/com/xgiovio/HashDictionaryChainingTest.java @@ -0,0 +1,88 @@ +package com.xgiovio; + +import dictionary.HashDictionaryChaining; +import dictionary.Logfile; +import exceptions.EmptyListException; +import priorityqueue.Entry; + +import java.security.InvalidKeyException; +import java.util.Iterator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 11/05/2014 + * Time: 21:17 + */ +public class HashDictionaryChainingTest { + + public static void main(String[] args) throws InvalidKeyException{ + + Logfile def = new Logfile(); + + HashDictionaryChaining h = new HashDictionaryChaining(def,5); + + System.out.println(h.load_factor()); + + h.insert("1", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + + h.insert("2", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("3", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("4", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("5", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("6", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("7", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("7", 200); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("7", 511); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("8", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("9", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + Entry t = h.insert("10", 100); + + System.out.println(h.load_factor()); + System.out.println(h.size()); + + + System.out.println(h); + System.out.println(h.raw_size()); + + + Iterator> ite = h.findAll("9").iterator(); + System.out.println(ite.next()); + + + } + +} diff --git a/com/xgiovio/HashDictionaryLinearProbingTest.java b/com/xgiovio/HashDictionaryLinearProbingTest.java new file mode 100644 index 0000000..d027bc2 --- /dev/null +++ b/com/xgiovio/HashDictionaryLinearProbingTest.java @@ -0,0 +1,102 @@ +package com.xgiovio; + +import dictionary.HashDictionaryLinearProbing; +import priorityqueue.Entry; +import priorityqueue.MyEntry; + +import java.security.InvalidKeyException; +import java.util.Iterator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 11/05/2014 + * Time: 21:17 + */ +public class HashDictionaryLinearProbingTest { + + public static void main(String[] args) throws InvalidKeyException{ + + MyEntry def = new MyEntry("null",0); + + HashDictionaryLinearProbing h = new HashDictionaryLinearProbing(def,5); + + System.out.println(h.load_factor()); + + h.insert("1", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + + h.insert("2", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("3", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("4", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("5", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("6", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("7", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("7", 200); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("7", 511); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("8", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + h.insert("9", 100); + System.out.println(h.load_factor()); + System.out.println(h.size()); + + Entry t = h.insert("10", 100); + + System.out.println(h.load_factor()); + System.out.println(h.size()); + + + System.out.println(h); + System.out.println(h.raw_size()); + + + Iterator> ite = h.findAll("7").iterator(); + System.out.println(ite.next()); + System.out.println(ite.next()); + System.out.println(ite.next()); + + System.out.println(h.load_factor()); + h.remove(t); + System.out.println(h); + System.out.println(h.load_factor()); + + + + Entry f = h.insert("7", 100); + System.out.println(h); + h.remove(f); + System.out.println(h); + + + + } + +} diff --git a/com/xgiovio/Main.java b/com/xgiovio/Main.java index 24454ef..b5c48d5 100644 --- a/com/xgiovio/Main.java +++ b/com/xgiovio/Main.java @@ -3,6 +3,6 @@ package com.xgiovio; public class Main { public static void main(String[] args) { - // write your code here + } } diff --git a/com/xgiovio/NodePositionListTest.java b/com/xgiovio/NodePositionListTest.java index e1f38e5..3a5bcee 100644 --- a/com/xgiovio/NodePositionListTest.java +++ b/com/xgiovio/NodePositionListTest.java @@ -1,7 +1,5 @@ package com.xgiovio; -import arraylist.ArrayIndexList; -import general_utility.test_object; import position.NodePositionList; import position.Position; diff --git a/com/xgiovio/NodeSequenceTest.java b/com/xgiovio/NodeSequenceTest.java index a242e28..4b6ad11 100644 --- a/com/xgiovio/NodeSequenceTest.java +++ b/com/xgiovio/NodeSequenceTest.java @@ -1,7 +1,6 @@ package com.xgiovio; -import arraylist.ArrayIndexList; -import general_utility.test_object; +import com.xgiovio.general_utility.test_object; import sequence.NodeSequence; /** diff --git a/com/xgiovio/QueueTest.java b/com/xgiovio/QueueTest.java index 9aedaae..78e9015 100644 --- a/com/xgiovio/QueueTest.java +++ b/com/xgiovio/QueueTest.java @@ -1,7 +1,6 @@ package com.xgiovio; -import general_utility.test_object; -import queue.ArrayQueue; +import com.xgiovio.general_utility.test_object; import queue.NodeQueue; /** diff --git a/general_utility/test_object.java b/com/xgiovio/general_utility/test_object.java similarity index 90% rename from general_utility/test_object.java rename to com/xgiovio/general_utility/test_object.java index 71a93c6..9349e89 100644 --- a/general_utility/test_object.java +++ b/com/xgiovio/general_utility/test_object.java @@ -1,4 +1,4 @@ -package general_utility; +package com.xgiovio.general_utility; /** * Created with xgiovio.macbookair. diff --git a/com/xgiovio/hash/functions.java b/com/xgiovio/hash/functions.java new file mode 100644 index 0000000..7d6514f --- /dev/null +++ b/com/xgiovio/hash/functions.java @@ -0,0 +1,31 @@ +package com.xgiovio.hash; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 11/05/2014 + * Time: 19:25 + */ +public class functions { + + + //http://m.smithworx.com/cs235/notes/view.php?file=extra%20notes/rodham/35-36-com.xgiovio.hash-tables/HashFunctions.java + public static int hash_horner( String key, int tableSize ) { + + int hashVal = 0; + + for ( int i = 0; i < key.length(); ++i ) { + hashVal = (hashVal * 37) + key.charAt( i ); + } + + hashVal %= tableSize; + + if ( hashVal < 0 ) { + hashVal += tableSize; + } + + return hashVal; + } + + +} diff --git a/dictionary/Dictionary.java b/dictionary/Dictionary.java new file mode 100644 index 0000000..bcfd78e --- /dev/null +++ b/dictionary/Dictionary.java @@ -0,0 +1,24 @@ +package dictionary; + +import exceptions.InvalidEntryException; +import priorityqueue.Entry; + +import java.security.InvalidKeyException; + + +public interface Dictionary { + + public int size(); + + public boolean isEmpty(); + + public Entry find(K key) throws InvalidKeyException; + + public Iterable> findAll(K key) throws InvalidKeyException; + + public Entry insert(K key, V value) throws InvalidKeyException; + + public Entry remove(Entry e) throws InvalidEntryException; + + public Iterable> entries(); +} diff --git a/dictionary/HashDictionaryChaining.java b/dictionary/HashDictionaryChaining.java new file mode 100644 index 0000000..bcaa415 --- /dev/null +++ b/dictionary/HashDictionaryChaining.java @@ -0,0 +1,192 @@ +package dictionary; + +import arraylist.ArrayIndexList; +import arraylist.IndexList; +import exceptions.HashExceptionKey; +import exceptions.InvalidEntryException; +import position.NodePositionList; +import position.PositionList; +import priorityqueue.Entry; +import priorityqueue.MyEntry; +import sun.rmi.log.ReliableLog; + +import java.security.InvalidKeyException; +import java.util.Iterator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 11/05/2014 + * Time: 19:28 + */ +public class HashDictionaryChaining implements Dictionary { + + IndexList> table = null; + Logfile AVAIBLE = null; + int avaible_slots = 0; + int elements = 0; + + public HashDictionaryChaining ( Logfile IN_AVAIBLE, int in_size){ + table = new ArrayIndexList>(in_size); + AVAIBLE = IN_AVAIBLE; + + for (int i= 0 ; i< in_size ;i++){ + table.add(i,AVAIBLE); + avaible_slots ++; + } + } + + public float load_factor () throws InvalidKeyException{ + float ret = (size() / (float)raw_size()); + if ( ret >= 0.9 ) { + reformat(); + return load_factor(); + } + return ret; + } + + public int raw_size (){ + return table.size(); + } + + @Override + public int size() { + return elements; + } + + @Override + public boolean isEmpty() { + return (size() == 0); + } + + protected int hash ( K key, int size){ + + if ( (key instanceof String) ) + return com.xgiovio.hash.functions.hash_horner( (String)key ,size ); + throw new HashExceptionKey(); + } + + + @Override + public Entry find(K key) throws InvalidKeyException { + + checkKey(key); + int h = hash(key, raw_size()); + if (table.get(h) == AVAIBLE) + return null; + Logfile n = table.get(h); + return n.find(key); + } + + @Override + public Iterable> findAll(K key) throws InvalidKeyException { + checkKey(key); + int h = hash(key,raw_size()); + if (table.get(h) == AVAIBLE) + return null; + Logfile n = table.get(h); + return n.entries(); + } + + @Override + public Entry insert(K key, V value) throws InvalidKeyException { + checkKey(key); + if (load_factor() >= 0.9 ) + reformat(); + int h = hash(key,raw_size()); + if (table.get(h) == AVAIBLE) { + Logfile n = new Logfile(); + table.set(h,n); + avaible_slots--; + elements++; + return n.insert(key,value); + } else { + Logfile n = table.get(h); + elements++; + return n.insert(key,value); + } + } + + @Override + public Entry remove(Entry e) throws InvalidEntryException { + if (e == null) + throw new InvalidEntryException(); + int h = hash(e.getKey(),raw_size()); + if (table.get(h) == AVAIBLE) + throw new InvalidEntryException(); + Logfile n = table.get(h); + Entry t = n.remove(e); + elements--; + if (n.size() == 0){ + table.set(h,AVAIBLE); + avaible_slots++; + } + return t; + } + + @Override + public Iterable> entries() { + + NodePositionList> ret = new NodePositionList>(); + + for (int i = 0 ; i< raw_size() ;i++){ + if (table.get(i) != AVAIBLE){ + Iterator> local_it = table.get(i).entries().iterator(); + for (;local_it.hasNext();){ + ret.addLast(local_it.next()); + } + } + + } + return ret; + } + + + protected void checkKey(K key) throws InvalidKeyException { + if (key == null ) + throw new InvalidKeyException("Invalid key"); + } + + + void reformat () throws InvalidKeyException{ + + Iterable> ite = entries(); + int new_size = raw_size() * 2; + IndexList> table2 = new ArrayIndexList>(new_size); + avaible_slots = 0; + elements = 0; + + for (int i= 0 ; i< (new_size) ;i++){ + table2.add(i,AVAIBLE); + } + table = table2; + + for (Entry e : ite){ + insert(e.getKey(),e.getValue()); + + } + } + + + public String toString() { + + Iterator> it = entries().iterator(); + + String to_return = ""; + to_return = to_return + "["; + + for (;it.hasNext();){ + Entry t = it.next(); + + if (it.hasNext()) { + + to_return+=(t.toString() + " , "); + } else { + + to_return+=(t.toString()); + } + } + + return to_return+= "]"; + } +} diff --git a/dictionary/HashDictionaryLinearProbing.java b/dictionary/HashDictionaryLinearProbing.java new file mode 100644 index 0000000..cb8d0eb --- /dev/null +++ b/dictionary/HashDictionaryLinearProbing.java @@ -0,0 +1,229 @@ +package dictionary; + +import arraylist.ArrayIndexList; +import arraylist.IndexList; +import exceptions.HashExceptionKey; +import exceptions.InvalidEntryException; +import position.NodePositionList; +import priorityqueue.Entry; +import priorityqueue.MyEntry; + +import java.security.InvalidKeyException; +import java.util.Iterator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 11/05/2014 + * Time: 19:28 + */ +public class HashDictionaryLinearProbing implements Dictionary { + + IndexList> table = null; + Entry AVAIBLE = null; + int avaible_slots = 0; + int elements = 0; + + public HashDictionaryLinearProbing(Entry IN_AVAIBLE, int in_size){ + table = new ArrayIndexList>(in_size); + AVAIBLE = IN_AVAIBLE; + + for (int i= 0 ; i< in_size ;i++){ + table.add(i,AVAIBLE); + avaible_slots ++; + } + } + + public float load_factor () throws InvalidKeyException{ + float ret = (size() / (float)raw_size()); + if ( ret >= 0.5 ) { + reformat(); + return load_factor(); + } + return ret; + } + + public int raw_size (){ + return table.size(); + } + + @Override + public int size() { + return elements; + } + + @Override + public boolean isEmpty() { + return (size() == 0); + } + + protected int hash ( K key, int size){ + + if ( (key instanceof String) ) + return com.xgiovio.hash.functions.hash_horner( (String)key ,size ); + throw new HashExceptionKey(); + } + + + + @Override + public Entry find(K key) throws InvalidKeyException { + + checkKey(key); + int h = hash(key, raw_size()); + int count = 0; + int i = 0; + for (;count < raw_size();){ + if (table.get(h+i) == AVAIBLE) + return null; + if (table.get(h+i).getKey().equals(key)) + return table.get(h+i); + i= (i + 1)% raw_size(); + count++; + } + return null; + } + + @Override + public Iterable> findAll(K key) throws InvalidKeyException { + checkKey(key); + NodePositionList> to_return = new NodePositionList>(); + int h = hash(key, raw_size()); + int count = 0; + int i = 0; + for (;count < raw_size();){ + if (table.get(h+i) == AVAIBLE) + return to_return; + if (table.get(h+i).getKey().equals(key)) + to_return.addLast( table.get(h+i)); + i= (i + 1)% raw_size(); + count++; + } + return to_return; + } + + @Override + public Entry insert(K key, V value) throws InvalidKeyException { + checkKey(key); + if (load_factor() >= 0.5 ) + reformat(); + int h = hash(key,raw_size()); + if (table.get(h) == AVAIBLE) { + MyEntry n = new MyEntry(key,value); + table.set(h,n); + avaible_slots--; + elements++; + return n; + } else { + int i = 1; + int count = 1; + for ( ; count < raw_size() ;) { + if (table.get(h + i) == AVAIBLE) { + MyEntry n = new MyEntry(key, value); + table.set(h + i, n); + avaible_slots--; + elements++; + return n; + } + i = (i + 1) % raw_size(); + count++; + } + return null; // non dovresti essere qui a causa del load factor + } + } + + @Override + public Entry remove(Entry e) throws InvalidEntryException { + if (e == null) + throw new InvalidEntryException(); + int h = hash(e.getKey(),raw_size()); + if (table.get(h) == AVAIBLE) + throw new InvalidEntryException(); + if (table.get(h) == e) { + Entry removed = table.get(h); + table.remove(h); + table.set(h,AVAIBLE); + elements--; + avaible_slots++; + return removed; + } + int count = 1; + for (int i = 1; count < raw_size() ; i = (i + 1) % raw_size()){ + if (table.get(h + i) == AVAIBLE) + throw new InvalidEntryException(); + if (table.get(h + i) == e) { + Entry removed = table.get(h + i); + table.remove(h + i); + table.set(h + i,AVAIBLE); + elements--; + avaible_slots++; + return removed; + } + count++; + } + throw new InvalidEntryException(); + + } + + @Override + public Iterable> entries() { + + NodePositionList> ret = new NodePositionList>(); + + for (int i = 0 ; i< raw_size() ;i++){ + if (table.get(i) != AVAIBLE){ + ret.addLast(table.get(i)); + } + } + return ret; + } + + + protected void checkKey(K key) throws InvalidKeyException { + if (key == null ) + throw new InvalidKeyException("Invalid key"); + } + + + void reformat () throws InvalidKeyException{ + + Iterable> ite = entries(); + int new_size = raw_size() * 2; + IndexList> table2 = new ArrayIndexList>(new_size); + avaible_slots = 0; + elements = 0; + + for (int i= 0 ; i< (new_size) ;i++){ + table2.add(i,AVAIBLE); + } + table = table2; + + for (Entry e : ite){ + insert(e.getKey(),e.getValue()); + + } + } + + + public String toString() { + + Iterator> it = entries().iterator(); + + String to_return = ""; + to_return = to_return + "["; + + for (;it.hasNext();){ + Entry t = it.next(); + + if (it.hasNext()) { + + to_return+=(t.toString() + " , "); + } else { + + to_return+=(t.toString()); + } + } + + return to_return+= "]"; + } +} diff --git a/dictionary/Logfile.java b/dictionary/Logfile.java new file mode 100644 index 0000000..712bc01 --- /dev/null +++ b/dictionary/Logfile.java @@ -0,0 +1,131 @@ +package dictionary; + +import exceptions.InvalidEntryException; +import position.NodePositionList; +import position.Position; +import priorityqueue.Entry; +import priorityqueue.MyEntry; + +import java.security.InvalidKeyException; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 11/05/2014 + * Time: 19:04 + */ +public class Logfile implements Dictionary { + + NodePositionList> list = new NodePositionList>(); + + @Override + public int size() { + return list.size(); + } + + @Override + public boolean isEmpty() { + return list.isEmpty(); + } + + @Override + public Entry find(K key) throws InvalidKeyException { + checkKey(key); + for(Position> p: list.positions()){ + Entry e= p.element(); + if(e.getKey().equals(key)){ + return e; + } + } + return null; + } + + @Override + public Iterable> findAll(K key) throws InvalidKeyException { + checkKey(key); + NodePositionList> to_return = new NodePositionList>(); + for(Position> p: list.positions()){ + Entry e= p.element(); + if(e.getKey().equals(key)){ + to_return.addLast(e); + } + } + return to_return; + } + + @Override + public Entry insert(K key, V value) throws InvalidKeyException { + checkKey(key); + LocationAwareEntry t = new LocationAwareEntry(key,value); + list.addLast(t); + t.setLocation(list.last()); + return t; + } + + @Override + public Entry remove(Entry e) throws InvalidEntryException { + LocationAwareEntry t = checkEntry(e); + list.remove(t.location()); + t.setLocation(null); + return t; + } + + @Override + public Iterable> entries() { + return list; + } + + //aux + protected void checkKey(K key) throws InvalidKeyException { + if (key == null ) + throw new InvalidKeyException("Invalid key"); + } + + // 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 + ")"; } + + + + } + + + protected LocationAwareEntry checkEntry(Entry e) throws InvalidEntryException + { + if(e == null || !(e instanceof LocationAwareEntry) || isEmpty() ) + throw new InvalidEntryException("entrata non valida"); + return (LocationAwareEntry) e; + } +} diff --git a/exceptions/HashExceptionKey.java b/exceptions/HashExceptionKey.java new file mode 100644 index 0000000..04c1d4c --- /dev/null +++ b/exceptions/HashExceptionKey.java @@ -0,0 +1,18 @@ +package exceptions; + +/** + * Signals that the boundaries of a data structure have been illegally + * traversed (e.g. past the end of a list). + * @author Roberto Tamassia + */ +//Copyright (c) 2003 Brown University, Providence, RI +//Additional modifications and methods by xgiovio + +public class HashExceptionKey extends RuntimeException { + public HashExceptionKey(String message) { + super (message); + } + public HashExceptionKey() { + super ("Please use a string key or define an hash function"); + } +} diff --git a/map/ListMap.java b/map/ListMap.java new file mode 100644 index 0000000..3adf21d --- /dev/null +++ b/map/ListMap.java @@ -0,0 +1,113 @@ +package map; + +import position.NodePositionList; +import position.Position; +import position.PositionList; +import priorityqueue.Entry; +import priorityqueue.MyEntry; + +import java.security.InvalidKeyException; +import java.util.Iterator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 11/05/2014 + * Time: 18:01 + */ +public class ListMap implements Map { + + private PositionList> L; + + public ListMap(){ + L =new NodePositionList>(); + } + + @Override + public int size() { + return L.size(); + } + + @Override + public boolean isEmpty() { + return (L.isEmpty()); + } + + + + + public V put(K key, V value) throws InvalidKeyException { + checkKey(key); + for(Position> p: L.positions()){ + Entry e= p.element(); + if(e.getKey().equals(key)){ + V v = e.getValue(); + L.set(p, new MyEntry(key,value)); + return v; + } } + L.addLast(new MyEntry(key,value)); + return null; + } + + public V get(K key) throws InvalidKeyException{ + checkKey(key); + for(Position> p: L.positions()){ + Entry e= p.element(); + if(e.getKey().equals(key)) return e.getValue(); + } + return null; + } + + + public V remove(K key) throws InvalidKeyException { + checkKey(key); + for(Position> p: L.positions()){ + Entry e= p.element(); + if(e.getKey().equals(key)){ + V v = e.getValue(); + L.remove(p); + return v; + } + } + return null; + } + + @Override + public Iterable keys() { + Iterator> it = L.iterator(); + NodePositionList ret = new NodePositionList(); + for (;it.hasNext();){ + ret.addLast(it.next().getKey()); + } + return ret; + } + + @Override + public Iterable values() { + Iterator> it = L.iterator(); + NodePositionList ret = new NodePositionList(); + for (;it.hasNext();){ + ret.addLast(it.next().getValue()); + } + return ret; + } + + @Override + public Iterable> entries() { + return L; + } + + + + // auxiliar + protected void checkKey(K key) throws InvalidKeyException { + if (key == null ) + throw new InvalidKeyException("Invalid key"); + } + + + @Override + public String toString() { + return L.toString(); + } +} diff --git a/map/Map.java b/map/Map.java new file mode 100644 index 0000000..7361e96 --- /dev/null +++ b/map/Map.java @@ -0,0 +1,25 @@ +package map; + +import priorityqueue.Entry; + +import java.security.InvalidKeyException; + +public interface Map { + + public int size(); + + public boolean isEmpty(); + + public V put(K key, V value) throws InvalidKeyException; + + public V get(K key) throws InvalidKeyException; + + public V remove(K key) throws InvalidKeyException; + + public Iterable keys(); + + public Iterable values(); + + public Iterable> entries(); +} +