Implementato HashCode Dictionary via LinearProbing (Open Adress) e Chaining.
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
package com.xgiovio;
|
package com.xgiovio;
|
||||||
|
|
||||||
import arraylist.ArrayIndexList;
|
import arraylist.ArrayIndexList;
|
||||||
import general_utility.test_object;
|
import com.xgiovio.general_utility.test_object;
|
||||||
import sequence.NodeSequence;
|
|
||||||
import sequence.Sequence;
|
|
||||||
import stack.NodeStack;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.xgiovio;
|
package com.xgiovio;
|
||||||
|
|
||||||
import general_utility.test_object;
|
import com.xgiovio.general_utility.test_object;
|
||||||
import position.Position;
|
import position.Position;
|
||||||
import sequence.ArraySequence;
|
import sequence.ArraySequence;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.xgiovio;
|
package com.xgiovio;
|
||||||
|
|
||||||
import general_utility.test_object;
|
import com.xgiovio.general_utility.test_object;
|
||||||
import stack.ArrayStack;
|
|
||||||
import stack.NodeStack;
|
import stack.NodeStack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package com.xgiovio;
|
package com.xgiovio;
|
||||||
|
|
||||||
import deque.Deque;
|
|
||||||
import deque.NodeDeque;
|
import deque.NodeDeque;
|
||||||
import general_utility.test_object;
|
import com.xgiovio.general_utility.test_object;
|
||||||
import stack.NodeStack;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created with xgiovio.macbookair.
|
* Created with xgiovio.macbookair.
|
||||||
|
|||||||
88
com/xgiovio/HashDictionaryChainingTest.java
Normal file
88
com/xgiovio/HashDictionaryChainingTest.java
Normal file
@@ -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<String,Integer> def = new Logfile<String, Integer>();
|
||||||
|
|
||||||
|
HashDictionaryChaining<String,Integer> h = new HashDictionaryChaining<String, Integer>(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<String,Integer> 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<Entry<String,Integer>> ite = h.findAll("9").iterator();
|
||||||
|
System.out.println(ite.next());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
102
com/xgiovio/HashDictionaryLinearProbingTest.java
Normal file
102
com/xgiovio/HashDictionaryLinearProbingTest.java
Normal file
@@ -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<String,Integer> def = new MyEntry<String, Integer>("null",0);
|
||||||
|
|
||||||
|
HashDictionaryLinearProbing<String,Integer> 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<String,Integer> 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<Entry<String,Integer>> 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<String,Integer> f = h.insert("7", 100);
|
||||||
|
System.out.println(h);
|
||||||
|
h.remove(f);
|
||||||
|
System.out.println(h);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,6 +3,6 @@ package com.xgiovio;
|
|||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// write your code here
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.xgiovio;
|
package com.xgiovio;
|
||||||
|
|
||||||
import arraylist.ArrayIndexList;
|
|
||||||
import general_utility.test_object;
|
|
||||||
import position.NodePositionList;
|
import position.NodePositionList;
|
||||||
import position.Position;
|
import position.Position;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.xgiovio;
|
package com.xgiovio;
|
||||||
|
|
||||||
import arraylist.ArrayIndexList;
|
import com.xgiovio.general_utility.test_object;
|
||||||
import general_utility.test_object;
|
|
||||||
import sequence.NodeSequence;
|
import sequence.NodeSequence;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.xgiovio;
|
package com.xgiovio;
|
||||||
|
|
||||||
import general_utility.test_object;
|
import com.xgiovio.general_utility.test_object;
|
||||||
import queue.ArrayQueue;
|
|
||||||
import queue.NodeQueue;
|
import queue.NodeQueue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package general_utility;
|
package com.xgiovio.general_utility;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created with xgiovio.macbookair.
|
* Created with xgiovio.macbookair.
|
||||||
31
com/xgiovio/hash/functions.java
Normal file
31
com/xgiovio/hash/functions.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
24
dictionary/Dictionary.java
Normal file
24
dictionary/Dictionary.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package dictionary;
|
||||||
|
|
||||||
|
import exceptions.InvalidEntryException;
|
||||||
|
import priorityqueue.Entry;
|
||||||
|
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
|
||||||
|
|
||||||
|
public interface Dictionary<K,V> {
|
||||||
|
|
||||||
|
public int size();
|
||||||
|
|
||||||
|
public boolean isEmpty();
|
||||||
|
|
||||||
|
public Entry<K,V> find(K key) throws InvalidKeyException;
|
||||||
|
|
||||||
|
public Iterable<Entry<K,V>> findAll(K key) throws InvalidKeyException;
|
||||||
|
|
||||||
|
public Entry<K,V> insert(K key, V value) throws InvalidKeyException;
|
||||||
|
|
||||||
|
public Entry<K,V> remove(Entry<K, V> e) throws InvalidEntryException;
|
||||||
|
|
||||||
|
public Iterable<Entry<K,V>> entries();
|
||||||
|
}
|
||||||
192
dictionary/HashDictionaryChaining.java
Normal file
192
dictionary/HashDictionaryChaining.java
Normal file
@@ -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 <K,V> implements Dictionary<K,V> {
|
||||||
|
|
||||||
|
IndexList<Logfile<K,V>> table = null;
|
||||||
|
Logfile<K,V> AVAIBLE = null;
|
||||||
|
int avaible_slots = 0;
|
||||||
|
int elements = 0;
|
||||||
|
|
||||||
|
public HashDictionaryChaining ( Logfile<K,V> IN_AVAIBLE, int in_size){
|
||||||
|
table = new ArrayIndexList<Logfile<K, V>>(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<K, V> find(K key) throws InvalidKeyException {
|
||||||
|
|
||||||
|
checkKey(key);
|
||||||
|
int h = hash(key, raw_size());
|
||||||
|
if (table.get(h) == AVAIBLE)
|
||||||
|
return null;
|
||||||
|
Logfile<K, V> n = table.get(h);
|
||||||
|
return n.find(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<Entry<K, V>> findAll(K key) throws InvalidKeyException {
|
||||||
|
checkKey(key);
|
||||||
|
int h = hash(key,raw_size());
|
||||||
|
if (table.get(h) == AVAIBLE)
|
||||||
|
return null;
|
||||||
|
Logfile<K, V> n = table.get(h);
|
||||||
|
return n.entries();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Entry<K, V> 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<K, V> n = new Logfile<K, V>();
|
||||||
|
table.set(h,n);
|
||||||
|
avaible_slots--;
|
||||||
|
elements++;
|
||||||
|
return n.insert(key,value);
|
||||||
|
} else {
|
||||||
|
Logfile<K, V> n = table.get(h);
|
||||||
|
elements++;
|
||||||
|
return n.insert(key,value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Entry<K, V> remove(Entry<K, V> 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<K, V> n = table.get(h);
|
||||||
|
Entry<K,V> t = n.remove(e);
|
||||||
|
elements--;
|
||||||
|
if (n.size() == 0){
|
||||||
|
table.set(h,AVAIBLE);
|
||||||
|
avaible_slots++;
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<Entry<K, V>> entries() {
|
||||||
|
|
||||||
|
NodePositionList<Entry<K,V>> ret = new NodePositionList<Entry<K, V>>();
|
||||||
|
|
||||||
|
for (int i = 0 ; i< raw_size() ;i++){
|
||||||
|
if (table.get(i) != AVAIBLE){
|
||||||
|
Iterator<Entry<K,V>> 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<Entry<K,V>> ite = entries();
|
||||||
|
int new_size = raw_size() * 2;
|
||||||
|
IndexList<Logfile<K,V>> table2 = new ArrayIndexList<Logfile<K, V>>(new_size);
|
||||||
|
avaible_slots = 0;
|
||||||
|
elements = 0;
|
||||||
|
|
||||||
|
for (int i= 0 ; i< (new_size) ;i++){
|
||||||
|
table2.add(i,AVAIBLE);
|
||||||
|
}
|
||||||
|
table = table2;
|
||||||
|
|
||||||
|
for (Entry<K,V> e : ite){
|
||||||
|
insert(e.getKey(),e.getValue());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
|
||||||
|
Iterator<Entry<K,V>> it = entries().iterator();
|
||||||
|
|
||||||
|
String to_return = "";
|
||||||
|
to_return = to_return + "[";
|
||||||
|
|
||||||
|
for (;it.hasNext();){
|
||||||
|
Entry<K,V> t = it.next();
|
||||||
|
|
||||||
|
if (it.hasNext()) {
|
||||||
|
|
||||||
|
to_return+=(t.toString() + " , ");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
to_return+=(t.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return to_return+= "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
229
dictionary/HashDictionaryLinearProbing.java
Normal file
229
dictionary/HashDictionaryLinearProbing.java
Normal file
@@ -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<K,V> implements Dictionary<K,V> {
|
||||||
|
|
||||||
|
IndexList<Entry<K,V>> table = null;
|
||||||
|
Entry<K,V> AVAIBLE = null;
|
||||||
|
int avaible_slots = 0;
|
||||||
|
int elements = 0;
|
||||||
|
|
||||||
|
public HashDictionaryLinearProbing(Entry<K, V> IN_AVAIBLE, int in_size){
|
||||||
|
table = new ArrayIndexList<Entry<K, V>>(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<K, V> 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<Entry<K, V>> findAll(K key) throws InvalidKeyException {
|
||||||
|
checkKey(key);
|
||||||
|
NodePositionList<Entry<K,V>> to_return = new NodePositionList<Entry<K, V>>();
|
||||||
|
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<K, V> 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<K, V> n = new MyEntry<K, V>(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<K, V> n = new MyEntry<K, V>(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<K, V> remove(Entry<K, V> 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<K, V> 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<K, V> 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<Entry<K, V>> entries() {
|
||||||
|
|
||||||
|
NodePositionList<Entry<K,V>> ret = new NodePositionList<Entry<K, V>>();
|
||||||
|
|
||||||
|
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<Entry<K,V>> ite = entries();
|
||||||
|
int new_size = raw_size() * 2;
|
||||||
|
IndexList<Entry<K,V>> table2 = new ArrayIndexList<Entry<K, V>>(new_size);
|
||||||
|
avaible_slots = 0;
|
||||||
|
elements = 0;
|
||||||
|
|
||||||
|
for (int i= 0 ; i< (new_size) ;i++){
|
||||||
|
table2.add(i,AVAIBLE);
|
||||||
|
}
|
||||||
|
table = table2;
|
||||||
|
|
||||||
|
for (Entry<K,V> e : ite){
|
||||||
|
insert(e.getKey(),e.getValue());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
|
||||||
|
Iterator<Entry<K,V>> it = entries().iterator();
|
||||||
|
|
||||||
|
String to_return = "";
|
||||||
|
to_return = to_return + "[";
|
||||||
|
|
||||||
|
for (;it.hasNext();){
|
||||||
|
Entry<K,V> t = it.next();
|
||||||
|
|
||||||
|
if (it.hasNext()) {
|
||||||
|
|
||||||
|
to_return+=(t.toString() + " , ");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
to_return+=(t.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return to_return+= "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
131
dictionary/Logfile.java
Normal file
131
dictionary/Logfile.java
Normal file
@@ -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<K,V> implements Dictionary<K,V> {
|
||||||
|
|
||||||
|
NodePositionList<Entry<K,V>> list = new NodePositionList<Entry<K,V>>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return list.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return list.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Entry<K, V> find(K key) throws InvalidKeyException {
|
||||||
|
checkKey(key);
|
||||||
|
for(Position<Entry<K,V>> p: list.positions()){
|
||||||
|
Entry<K,V> e= p.element();
|
||||||
|
if(e.getKey().equals(key)){
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<Entry<K, V>> findAll(K key) throws InvalidKeyException {
|
||||||
|
checkKey(key);
|
||||||
|
NodePositionList<Entry<K,V>> to_return = new NodePositionList<Entry<K,V>>();
|
||||||
|
for(Position<Entry<K,V>> p: list.positions()){
|
||||||
|
Entry<K,V> e= p.element();
|
||||||
|
if(e.getKey().equals(key)){
|
||||||
|
to_return.addLast(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return to_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Entry<K, V> insert(K key, V value) throws InvalidKeyException {
|
||||||
|
checkKey(key);
|
||||||
|
LocationAwareEntry<K,V> t = new LocationAwareEntry<K, V>(key,value);
|
||||||
|
list.addLast(t);
|
||||||
|
t.setLocation(list.last());
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
|
||||||
|
LocationAwareEntry<K,V> t = checkEntry(e);
|
||||||
|
list.remove(t.location());
|
||||||
|
t.setLocation(null);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<Entry<K, V>> 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<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 + ")"; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
exceptions/HashExceptionKey.java
Normal file
18
exceptions/HashExceptionKey.java
Normal file
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
113
map/ListMap.java
Normal file
113
map/ListMap.java
Normal file
@@ -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<K,V> implements Map<K,V> {
|
||||||
|
|
||||||
|
private PositionList<Entry<K,V>> L;
|
||||||
|
|
||||||
|
public ListMap(){
|
||||||
|
L =new NodePositionList<Entry<K,V>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<Entry<K,V>> p: L.positions()){
|
||||||
|
Entry<K,V> e= p.element();
|
||||||
|
if(e.getKey().equals(key)){
|
||||||
|
V v = e.getValue();
|
||||||
|
L.set(p, new MyEntry<K,V>(key,value));
|
||||||
|
return v;
|
||||||
|
} }
|
||||||
|
L.addLast(new MyEntry<K,V>(key,value));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public V get(K key) throws InvalidKeyException{
|
||||||
|
checkKey(key);
|
||||||
|
for(Position<Entry<K,V>> p: L.positions()){
|
||||||
|
Entry<K,V> e= p.element();
|
||||||
|
if(e.getKey().equals(key)) return e.getValue();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public V remove(K key) throws InvalidKeyException {
|
||||||
|
checkKey(key);
|
||||||
|
for(Position<Entry<K,V>> p: L.positions()){
|
||||||
|
Entry<K,V> e= p.element();
|
||||||
|
if(e.getKey().equals(key)){
|
||||||
|
V v = e.getValue();
|
||||||
|
L.remove(p);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<K> keys() {
|
||||||
|
Iterator<Entry<K,V>> it = L.iterator();
|
||||||
|
NodePositionList<K> ret = new NodePositionList<K>();
|
||||||
|
for (;it.hasNext();){
|
||||||
|
ret.addLast(it.next().getKey());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<V> values() {
|
||||||
|
Iterator<Entry<K,V>> it = L.iterator();
|
||||||
|
NodePositionList<V> ret = new NodePositionList<V>();
|
||||||
|
for (;it.hasNext();){
|
||||||
|
ret.addLast(it.next().getValue());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<Entry<K, V>> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
25
map/Map.java
Normal file
25
map/Map.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package map;
|
||||||
|
|
||||||
|
import priorityqueue.Entry;
|
||||||
|
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
|
||||||
|
public interface Map<K,V> {
|
||||||
|
|
||||||
|
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<K> keys();
|
||||||
|
|
||||||
|
public Iterable<V> values();
|
||||||
|
|
||||||
|
public Iterable<Entry<K,V>> entries();
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user