5cb944cdb33fa71faa68f75f7faa6d961d3f7c25
[IRC.git] / Robust / src / ClassLibrary / JavaDSM / DistributedHashMap.java
1 public class DistributedHashMap {
2   DistributedHashEntry[] table;
3   float loadFactor;
4   int secondcapacity;
5         int size;
6
7   public DistributedHashMap(int initialCapacity, int secondcapacity, float loadFactor) {
8     init(initialCapacity, secondcapacity, loadFactor);
9                 size = 0;
10   }
11
12   private void init(int initialCapacity, int secondcapacity, float loadFactor) {
13     table=global new DistributedHashEntry[initialCapacity];
14     this.loadFactor=loadFactor;
15     this.secondcapacity=secondcapacity;
16   }
17
18   private static int hash1(int hashcode, int length) {
19     int value=hashcode%length;
20     if (value<0)
21       return -value;
22     else
23       return value;
24   }
25
26   private static int hash2(int hashcode, int length1, int length2) {
27     int value=(hashcode*31)%length2;
28     if (value<0)
29       return -value;
30     else
31       return value;
32   }
33
34   void resize(int index) {
35     DHashEntry[] oldtable=table[index].array;
36     int newCapacity=oldtable.length*2+1;
37     DHashEntry [] newtable=global new DHashEntry[newCapacity];
38     table[index].array=newtable;
39
40     for(int i=0; i<oldtable.length; i++) {
41       DHashEntry e=oldtable[i];
42       while(e!=null) {
43                                 DHashEntry next=e.next;
44                                 int bin=hash2(e.hashval, table.length, newCapacity);
45                                 e.next=newtable[bin];
46                                 newtable[bin]=e;
47                                 e=next;
48       }
49     }
50   }
51
52   Object remove(Object key) {
53     int hashcode=key.hashCode();
54     int index1=hash1(hashcode, table.length);
55     DistributedHashEntry dhe=table[index1];
56     if (dhe==null)
57       return null;
58     int index2=hash2(hashcode, table.length, dhe.array.length);
59     DHashEntry ptr=dhe.array[index2];
60
61     if (ptr!=null) {
62       if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
63                                 dhe.array[index2]=ptr.next;
64                                 dhe.count--;
65                                 size--;
66                                 return ptr.value;
67       }
68       while(ptr.next!=null) {
69                                 if (ptr.hashval==hashcode&&ptr.next.key.equals(key)) {
70                                         Object oldvalue=ptr.value;
71                                         ptr.next=ptr.next.next;
72                                         dhe.count--;
73                                         size--;
74                                         return oldvalue;
75                                 }
76                                 ptr=ptr.next;
77                         }
78     }
79     return null;
80   }
81
82   Object get(Object key) {
83     int hashcode=key.hashCode();
84     int index1=hash1(hashcode, table.length);
85     DistributedHashEntry dhe=table[index1];
86     if (dhe==null)
87       return null;
88
89     int index2=hash2(hashcode, table.length, dhe.array.length);
90     DHashEntry ptr=dhe.array[index2];
91
92     while(ptr!=null) {
93       if (ptr.hashval==hashcode
94           &&ptr.key.equals(key)) {
95                                 return ptr.value;
96       }
97       ptr=ptr.next;
98     }
99     return null;
100   }
101
102   boolean containsKey(Object key) {
103     int hashcode=key.hashCode();
104     int index1=hash1(hashcode, table.length);
105     DistributedHashEntry dhe=table[index1];
106     if (dhe==null)
107       return false;
108     int index2=hash2(hashcode, table.length, dhe.array.length);
109     DHashEntry ptr=dhe.array[index2];
110
111     while(ptr!=null) {
112       if (ptr.hashval==hashcode
113           &&ptr.key.equals(key)) {
114                                 return true;
115       }
116       ptr=ptr.next;
117     }
118     return false;
119   }
120
121   Object put(Object key, Object value) {
122     int hashcode=key.hashCode();
123     int index1=hash1(hashcode, table.length);
124     DistributedHashEntry dhe=table[index1];
125     if (dhe==null) {
126       dhe=global new DistributedHashEntry(secondcapacity);
127       table[index1]=dhe;
128     }
129     int index2=hash2(hashcode, table.length, dhe.array.length);
130     DHashEntry ptr=dhe.array[index2];
131
132     while(ptr!=null) {
133       if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
134                                 Object oldvalue=ptr.value;
135                                 ptr.value=value;
136                                 return oldvalue;
137       }
138       ptr=ptr.next;
139     }
140
141     DHashEntry he=global new DHashEntry();
142     he.value=value;
143     he.key=key;
144     he.hashval=hashcode;
145     he.next=dhe.array[index2];
146     dhe.array[index2]=he;
147
148     dhe.count++;
149     if (dhe.count>(loadFactor*dhe.array.length)) {
150       //Resize the table
151       resize(index1);
152     }
153                 size++;
154     return null;
155   }
156         
157         public int size() {
158                 return size;
159         }
160 }
161
162
163 class DistributedHashEntry {
164   public DistributedHashEntry(int capacity) {
165     array=global new DHashEntry[capacity];
166   }
167   int count;
168   DHashEntry[] array;
169 }
170
171
172 class DHashEntry {
173   public DHashEntry() {
174   }
175   int hashval;
176   Object key;
177   Object value;
178   DHashEntry next;
179 }