1 public class DistributedHashMap {
2 DistributedHashEntry[] table;
6 public DistributedHashMap(int initialCapacity, int secondcapacity, float loadFactor) {
7 init(initialCapacity, secondcapacity, loadFactor);
10 private void init(int initialCapacity, int secondcapacity, float loadFactor) {
11 table = global new DistributedHashEntry[initialCapacity];
12 this.loadFactor=loadFactor;
13 this.secondcapacity=secondcapacity;
16 private static int hash1(int hashcode, int length) {
17 int value=hashcode%length;
24 private static int hash2(int hashcode, int length1, int length2) {
25 int value=(hashcode*31)%length2;
32 void resize(int index) {
33 DHashEntry[] oldtable=table[index].array;
34 int newCapacity=oldtable.length*2+1;
35 DHashEntry [] newtable=global new DHashEntry[newCapacity];
36 table[index].array=newtable;
38 for(int i=0; i<oldtable.length; i++) {
39 DHashEntry e=oldtable[i];
41 DHashEntry next=e.next;
42 int bin=hash2(e.hashval, table.length, newCapacity);
50 Object remove(Object key) {
51 int hashcode=key.hashCode();
52 int index1=hash1(hashcode, table.length);
53 DistributedHashEntry dhe=table[index1];
56 int index2=hash2(hashcode, table.length, dhe.array.length);
57 DHashEntry ptr=dhe.array[index2];
60 if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
61 dhe.array[index2]=ptr.next;
65 while(ptr.next!=null) {
66 if (ptr.hashval==hashcode&&ptr.next.key.equals(key)) {
67 Object oldvalue=ptr.value;
68 ptr.next=ptr.next.next;
78 Object get(Object key) {
79 int hashcode=key.hashCode();
80 int index1=hash1(hashcode, table.length);
82 DistributedHashEntry dhe=table[index1];
86 int index2=hash2(hashcode, table.length, dhe.array.length);
88 DHashEntry ptr=dhe.array[index2];
91 if (ptr.hashval==hashcode
92 &&ptr.key.equals(key)) {
100 boolean containsKey(Object key) {
101 int hashcode=key.hashCode();
102 int index1=hash1(hashcode, table.length);
103 DistributedHashEntry dhe=table[index1];
106 int index2=hash2(hashcode, table.length, dhe.array.length);
107 DHashEntry ptr=dhe.array[index2];
110 if (ptr.hashval==hashcode
111 &&ptr.key.equals(key)) {
119 Object put(Object key, Object value) {
120 int hashcode=key.hashCode();
121 int index1=hash1(hashcode, table.length);
122 DistributedHashEntry dhe=table[index1];
124 dhe=global new DistributedHashEntry(secondcapacity);
127 int index2=hash2(hashcode, table.length, dhe.array.length);
128 DHashEntry ptr=dhe.array[index2];
131 if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
132 Object oldvalue=ptr.value;
139 DHashEntry he=global new DHashEntry();
143 he.next=dhe.array[index2];
144 dhe.array[index2]=he;
147 if (dhe.count>(loadFactor*dhe.array.length)) {
156 class DistributedHashEntry {
157 public DistributedHashEntry(int capacity) {
158 array=global new DHashEntry[capacity];
166 public DHashEntry() {