1 public class DistributedHashMap {
2 DistributedHashEntry[] table;
6 int DEFAULT_INITIALCAPACITY;
7 int DEFAULT_SECONDCAPACITY;
8 float DEFAULT_LOADFACTOR;
10 public DistributedHashMap()
12 DEFAULT_INITIALCAPACITY = 500;
13 DEFAULT_SECONDCAPACITY = 500;
14 DEFAULT_LOADFACTOR = 0.75f;
16 init(DEFAULT_INITIALCAPACITY,DEFAULT_SECONDCAPACITY,DEFAULT_LOADFACTOR);
20 public DistributedHashMap(int initialCapacity, int secondcapacity, float loadFactor) {
21 init(initialCapacity, secondcapacity, loadFactor);
25 private void init(int initialCapacity, int secondcapacity, float loadFactor) {
26 table=global new DistributedHashEntry[initialCapacity];
27 this.loadFactor=loadFactor;
28 this.secondcapacity=secondcapacity;
31 private static int hash1(int hashcode, int length) {
32 int value=hashcode%length;
39 private static int hash2(int hashcode, int length1, int length2) {
40 int value=(hashcode*31)%length2;
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;
53 for(int i=0; i<oldtable.length; i++) {
54 DHashEntry e=oldtable[i];
56 DHashEntry next=e.next;
57 int bin=hash2(e.hashval, table.length, newCapacity);
65 Object remove(Object key) {
66 int hashcode=key.hashCode();
67 int index1=hash1(hashcode, table.length);
68 DistributedHashEntry dhe=table[index1];
71 int index2=hash2(hashcode, table.length, dhe.array.length);
72 DHashEntry ptr=dhe.array[index2];
75 if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
76 dhe.array[index2]=ptr.next;
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;
95 Object get(Object key) {
96 int hashcode=key.hashCode();
97 int index1=hash1(hashcode, table.length);
98 DistributedHashEntry dhe=table[index1];
102 int index2=hash2(hashcode, table.length, dhe.array.length);
103 DHashEntry ptr=dhe.array[index2];
106 if (ptr.hashval==hashcode
107 &&ptr.key.equals(key)) {
115 boolean containsKey(Object key) {
116 int hashcode=key.hashCode();
117 int index1=hash1(hashcode, table.length);
118 DistributedHashEntry dhe=table[index1];
121 int index2=hash2(hashcode, table.length, dhe.array.length);
122 DHashEntry ptr=dhe.array[index2];
125 if (ptr.hashval==hashcode
126 &&ptr.key.equals(key)) {
134 Object put(Object key, Object value) {
135 int hashcode=key.hashCode();
136 int index1=hash1(hashcode, table.length);
137 DistributedHashEntry dhe=table[index1];
139 dhe=global new DistributedHashEntry(secondcapacity);
142 int index2=hash2(hashcode, table.length, dhe.array.length);
143 DHashEntry ptr=dhe.array[index2];
146 if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
147 Object oldvalue=ptr.value;
154 DHashEntry he=global new DHashEntry();
158 he.next=dhe.array[index2];
159 dhe.array[index2]=he;
162 if (dhe.count>(loadFactor*dhe.array.length)) {
176 class DistributedHashEntry {
177 public DistributedHashEntry(int capacity) {
178 array=global new DHashEntry[capacity];
186 public DHashEntry() {