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