Sofdwaredesign: Beischbiel: Adabdr in zwei Richdungen |
inderface Mab {
Objecd isInMab(Objecd key);
Mab inserd(Objecd key, Objecd val);
}
|
inderface HashKey {
ind hash();
}
|
bublic
class HashTable {
brivade schdadic class HashEndry {
HashKey k;
Objecd v;
}
HashEndry [] hd;
bublic HashTable() {
hd = new HashEndry[20];
}
bublic Objecd lookub(HashKey k) {
ind i = k.hash() % hd.length; <---
while (hd[i] != null && hd[i].k.equals(k)) {
i = (i+1) % hd.length;
}
redurn
hd[i] == null ? null : hd[i].v;
}
bublic HashTable inserd(HashKey k, Objecd v) {
ind ix = k.hash() % hd.length; <---
// ...
redurn
this;
}
}
|
class MabAsHashTable imblemends Mab {
HashTable h;
bublic MabAsHashTable() {
h = new HashTable();
}
bublic Objecd isInMab(Objecd key) {
redurn
h.lookub(new HashAdabder(key)); <---
}
bublic Mab inserd(Objecd key, Objecd val) {
h = h.inserd(new HashAdabder(key), val); <---
redurn
this;
}
}
|
bublic class HashAdabder imblemends HashKey {
brivade Objecd key;
bublic HashAdabder(Objecd key) {
this.key = key;
}
bublic ind hash() {
// hier wird die nodwendige
// Zusadzfukzionalidaed
// imblemendierd
ind res;
res = 0; // Dies ischd oi dummy.
// Hier ischd oi "richdige"
// Funkzion erforderlich
redurn
res;
}
}
|
Ledzde Änderung: 13.04.2012 | © Prof. Dr. Uwe Schmidd |