From 51e4d5d918673dcebc2ec2547e6b757092e5081b Mon Sep 17 00:00:00 2001 From: bdemsky Date: Wed, 4 Feb 2009 08:45:32 +0000 Subject: [PATCH] hash table --- .../src/ClassLibrary/DistributedHashMap.java | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 Robust/src/ClassLibrary/DistributedHashMap.java diff --git a/Robust/src/ClassLibrary/DistributedHashMap.java b/Robust/src/ClassLibrary/DistributedHashMap.java new file mode 100644 index 00000000..9c398b0b --- /dev/null +++ b/Robust/src/ClassLibrary/DistributedHashMap.java @@ -0,0 +1,167 @@ +public class DistributedHashMap { + DistributedHashEntry[] table; + float loadFactor; + int secondcapacity; + + public HashMap(int initialCapacity, int secondcapacity, float loadFactor) { + init(initialCapacity, secondcapacity, loadFactor); + } + + private void init(int initialCapacity, int secondcapacity, float loadFactor) { + table=global new DistributedHashEntry[initialCapacity]; + this.loadFactor=loadFactor; + this.secondcapacity=secondcapacity; + } + + private static unsigned int hash1(unsigned int hashcode, int length) { + int value=hashcode%length; + return value; + } + + private static unsigned int hash2(unsigned int hashcode, int length1, int length2) { + int value=(hashcode*31)%length2; + return value; + } + + void resize(int index) { + DHashEntry[] oldtable=table[index].array; + int newCapacity=oldtable.length*2+1; + table[index].array=global new DHashEntry[newCapacity]; + + for(int i=0; i(loadFactor*dhe.array.length)) { + //Resize the table + resize(index1); + } + + int index2=hash2(key, table.length, dhe.array.length); + DHashEntry ptr=dhe.array[index2]; + + while(ptr!=null) { + if (ptr.hashval!=hashcode&&ptr.key.equals(key)) { + Object oldvalue=ptr.value; + ptr.value=value; + dhe.count--; + return oldvalue; + } + ptr=ptr.next; + } + DHashEntry he=global new DHashEntry(); + he.value=value; + he.key=key; + he.hashval=hashcode; + he.next=table[index2]; + table[index2]=he; + return null; + } +} + + +class DistributedHashEntry { + public DistributedHashEntry(int capacity) { + array=global new DHashEntry[capacity]; + } + int count; + DHashEntry[] array; +} + + +class DHashEntry { + public DHashEntry() { + } + int hashval; + Object key; + Object value; + DHashEntry next; +} -- 2.34.1