1 public class DistributedHashMap {
2 DistributedHashEntry[] table;
6 public DistributedHashMap(int initialCapacity, int secondcapacity, float loadFactor) {
7 init(initialCapacity, secondcapacity, loadFactor);
10 private void init(int initialCapacity, int secondcapacity, float loadFactor) {
11 table=global new DistributedHashEntry[initialCapacity];
12 this.loadFactor=loadFactor;
13 this.secondcapacity=secondcapacity;
16 private static int hash1(int hashcode, int length) {
17 int value=hashcode%length;
24 private static int hash2(int hashcode, int length1, int length2) {
25 int value=(hashcode*31)%length2;
32 void resize(int index) {
33 DHashEntry[] oldtable=table[index].array;
34 int newCapacity=oldtable.length*2+1;
35 DHashEntry [] newtable=global new DHashEntry[newCapacity];
36 table[index].array=newtable;
38 for(int i=0; i<oldtable.length; i++) {
39 DHashEntry e=oldtable[i];
41 DHashEntry next=e.next;
42 int bin=hash2(e.hashval, table.length, newCapacity);
50 Object remove(Object key) {
51 int hashcode=key.hashCode();
52 int index1=hash1(hashcode, table.length);
53 DistributedHashEntry dhe=table[index1];
56 int index2=hash2(hashcode, table.length, dhe.array.length);
57 DHashEntry ptr=dhe.array[index2];
60 if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
61 dhe.array[index2]=ptr.next;
65 while(ptr.next!=null) {
66 if (ptr.hashval==hashcode&&ptr.next.key.equals(key)) {
67 Object oldvalue=ptr.value;
68 ptr.next=ptr.next.next;
78 Object get(Object key) {
79 int hashcode=key.hashCode();
80 int index1=hash1(hashcode, table.length);
81 DistributedHashEntry dhe=table[index1];
85 int index2=hash2(hashcode, table.length, dhe.array.length);
86 DHashEntry ptr=dhe.array[index2];
89 if (ptr.hashval==hashcode
90 &&ptr.key.equals(key)) {
98 boolean containsKey(Object key) {
99 int hashcode=key.hashCode();
100 int index1=hash1(hashcode, table.length);
101 DistributedHashEntry dhe=table[index1];
104 int index2=hash2(hashcode, table.length, dhe.array.length);
105 DHashEntry ptr=dhe.array[index2];
108 if (ptr.hashval==hashcode
109 &&ptr.key.equals(key)) {
117 Object put(Object key, Object value) {
118 int hashcode=key.hashCode();
119 int index1=hash1(hashcode, table.length);
120 DistributedHashEntry dhe=table[index1];
122 dhe=global new DistributedHashEntry(secondcapacity);
125 int index2=hash2(hashcode, table.length, dhe.array.length);
126 DHashEntry ptr=dhe.array[index2];
129 if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
130 Object oldvalue=ptr.value;
137 DHashEntry he=global new DHashEntry();
141 he.next=dhe.array[index2];
142 dhe.array[index2]=he;
145 if (dhe.count>(loadFactor*dhe.array.length)) {
158 class DistributedHashEntry {
159 public DistributedHashEntry(int capacity) {
160 array=global new DHashEntry[capacity];
168 public DHashEntry() {