0f147e5ac139febe7d26296cabbf426b7770c080
[IRC.git] / Robust / src / Benchmarks / Distributed / LookUpService / dsm2 / DistributedHashMap.java
1 public class DistributedHashMap {
2   DistributedHashEntry[] table;
3   float loadFactor;
4
5   public DistributedHashMap(int initialCapacity, float loadFactor) {
6     init(initialCapacity, loadFactor);
7   }
8
9   private void init(int initialCapacity, float loadFactor) {
10     table=global new DistributedHashEntry[initialCapacity];
11     this.loadFactor=loadFactor;
12   }
13
14   private static int hash1(int hashcode, int length) {
15     int value=hashcode%length;
16     if (value<0)
17       return -value;
18     else
19       return value;
20   }
21
22   Object remove(Object key) {
23     int hashcode=key.hashCode();
24     int index1=hash1(hashcode, table.length);
25     DistributedHashEntry dhe=table[index1];
26     if (dhe==null)
27       return null;
28     DHashEntry ptr=dhe.array;
29
30     if (ptr!=null) {
31       if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
32         dhe.array=ptr.next;
33         dhe.count--;
34         return ptr.value;
35       }
36       while(ptr.next!=null) {
37         if (ptr.hashval==hashcode&&ptr.next.key.equals(key)) {
38           Object oldvalue=ptr.value;
39           ptr.next=ptr.next.next;
40           dhe.count--;
41           return oldvalue;
42         }
43         ptr=ptr.next;
44       }
45     }
46     return null;
47   }
48
49   Object get(Object key) {
50     int hashcode=key.hashCode();
51     int index1=hash1(hashcode, table.length);
52     
53     DistributedHashEntry dhe=table[index1];
54     if (dhe==null)
55       return null;
56
57     /****** Add Manual Prefetch *****/
58     //dhe.array.next(5).key
59     Object obj1 = dhe.array;
60     short[] offsets1 = new short[4];
61     offsets1[0] = getoffset {DHashEntry, next};
62     offsets1[1] = (short) 2;
63     offsets1[2] = getoffset {DHashEntry, key};
64     offsets1[3] = (short) 0;
65     System.rangePrefetch(obj1, offsets1);
66     /********************************/
67
68     DHashEntry ptr=dhe.array;
69
70     while(ptr!=null) {
71       if (ptr.hashval==hashcode
72           &&ptr.key.equals(key)) {
73         return ptr.value;
74       }
75       ptr=ptr.next;
76     }
77     return null;
78   }
79
80   boolean containsKey(Object key) {
81     int hashcode=key.hashCode();
82     int index1=hash1(hashcode, table.length);
83     DistributedHashEntry dhe=table[index1];
84     if (dhe==null)
85       return false;
86
87     DHashEntry ptr=dhe.array;
88
89     while(ptr!=null) {
90       if (ptr.hashval==hashcode
91           &&ptr.key.equals(key)) {
92         return true;
93       }
94       ptr=ptr.next;
95     }
96     return false;
97   }
98
99   Object put(Object key, Object value) {
100     int hashcode=key.hashCode();
101     int index1=hash1(hashcode, table.length);
102     DistributedHashEntry dhe=table[index1];
103     if (dhe==null) {
104         dhe=global new DistributedHashEntry();
105         table[index1]=dhe;
106     }
107     DHashEntry ptr=dhe.array;
108
109     while(ptr!=null) {
110       if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
111         Object oldvalue=ptr.value;
112         ptr.value=value;
113         return oldvalue;
114       }
115       ptr=ptr.next;
116     }
117
118     DHashEntry he=global new DHashEntry();
119     he.value=value;
120     he.key=key;
121     he.hashval=hashcode;
122     he.next=dhe.array;
123     dhe.array=he;
124
125     dhe.count++;
126     return null;
127   }
128 }
129
130
131 class DistributedHashEntry {
132   public DistributedHashEntry() {
133   }
134   int count;
135   DHashEntry array;
136 }
137
138
139 class DHashEntry {
140   public DHashEntry() {
141   }
142   int hashval;
143   Object key;
144   Object value;
145   DHashEntry next;
146 }