1 public class DistributedHashMap {
2 DistributedHashEntry[] table;
7 public DistributedHashMap(int initialCapacity, int secondcapacity, float loadFactor) {
8 init(initialCapacity, secondcapacity, loadFactor);
12 private void init(int initialCapacity, int secondcapacity, float loadFactor) {
13 table=global new DistributedHashEntry[initialCapacity];
14 this.loadFactor=loadFactor;
15 this.secondcapacity=secondcapacity;
18 private static int hash1(int hashcode, int length) {
19 int value=hashcode%length;
26 private static int hash2(int hashcode, int length1, int length2) {
27 int value=(hashcode*31)%length2;
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;
40 for(int i=0; i<oldtable.length; i++) {
41 DHashEntry e=oldtable[i];
43 DHashEntry next=e.next;
44 int bin=hash2(e.hashval, table.length, newCapacity);
52 Object remove(Object key) {
53 int hashcode=key.hashCode();
54 int index1=hash1(hashcode, table.length);
55 DistributedHashEntry dhe=table[index1];
58 int index2=hash2(hashcode, table.length, dhe.array.length);
59 DHashEntry ptr=dhe.array[index2];
62 if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
63 dhe.array[index2]=ptr.next;
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;
82 Object get(Object key) {
83 int hashcode=key.hashCode();
84 int index1=hash1(hashcode, table.length);
85 DistributedHashEntry dhe=table[index1];
89 int index2=hash2(hashcode, table.length, dhe.array.length);
90 DHashEntry ptr=dhe.array[index2];
93 if (ptr.hashval==hashcode
94 &&ptr.key.equals(key)) {
102 boolean containsKey(Object key) {
103 int hashcode=key.hashCode();
104 int index1=hash1(hashcode, table.length);
105 DistributedHashEntry dhe=table[index1];
108 int index2=hash2(hashcode, table.length, dhe.array.length);
109 DHashEntry ptr=dhe.array[index2];
112 if (ptr.hashval==hashcode
113 &&ptr.key.equals(key)) {
121 Object put(Object key, Object value) {
122 int hashcode=key.hashCode();
123 int index1=hash1(hashcode, table.length);
124 DistributedHashEntry dhe=table[index1];
126 dhe=global new DistributedHashEntry(secondcapacity);
129 int index2=hash2(hashcode, table.length, dhe.array.length);
130 DHashEntry ptr=dhe.array[index2];
133 if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
134 Object oldvalue=ptr.value;
141 DHashEntry he=global new DHashEntry();
145 he.next=dhe.array[index2];
146 dhe.array[index2]=he;
149 if (dhe.count>(loadFactor*dhe.array.length)) {
163 class DistributedHashEntry {
164 public DistributedHashEntry(int capacity) {
165 array=global new DHashEntry[capacity];
173 public DHashEntry() {