Implementato HashCode Dictionary via LinearProbing (Open Adress) e Chaining.
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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.
|
||||
|
||||
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 static void main(String[] args) {
|
||||
// write your code here
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.xgiovio;
|
||||
|
||||
import arraylist.ArrayIndexList;
|
||||
import general_utility.test_object;
|
||||
import position.NodePositionList;
|
||||
import position.Position;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
26
com/xgiovio/general_utility/test_object.java
Normal file
26
com/xgiovio/general_utility/test_object.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.xgiovio.general_utility;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 10/03/14
|
||||
* Time: 16:41
|
||||
*/
|
||||
public class test_object {
|
||||
|
||||
public test_object (int in_number){
|
||||
number = in_number;
|
||||
}
|
||||
|
||||
public int get_data (){
|
||||
return number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(number);
|
||||
}
|
||||
|
||||
private int number;
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user