-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.java
More file actions
116 lines (93 loc) · 2.15 KB
/
Copy pathHashTable.java
File metadata and controls
116 lines (93 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
public class HashTable {
public HashTable(int size) {
int i;
hsize = size;
table = new HashElem[hsize];
for (i=0; i<size;i++)
table[i] = null;
marker = "StackMarker";
numelements_ = 0;
}
public void insert(String key, Object data) {
int hashval = hash(key);
table[hashval] = new HashElem(key, data, table[hashval]);
numelements_++;
stack = new StackElem(key, stack);
}
public int numelements() {
return numelements_;
}
public Object find(String key) {
HashElem tmp = table[hash(key)];
while (tmp!= null && key.compareTo(tmp.key) != 0)
tmp = tmp.next;
if (tmp == null)
return null;
return tmp.data;
}
public void beginScope() {
stack = new StackElem(marker,stack);
}
public void endScope() {
while (stack != null && stack.key != marker) {
delete(stack.key);
stack = stack.next;
}
if (stack != null)
stack = stack.next;
}
public void delete(String key) {
int index;
HashElem tmp;
index = hash(key);
if (table[index] != null) {
if (key.compareTo(table[index].key) == 0) {
table[index] = table[index].next;
numelements_--;
} else {
for (tmp = table[index]; (tmp.next != null &&
key.compareTo(tmp.next.key) != 0); tmp = tmp.next);
if (tmp.next != null) {
tmp.next = tmp.next.next;
numelements_--;
}
}
}
}
private int hash(String key) {
long h = 0;
long g;
int i;
for(i=0; i<key.length(); i++) {
h = h << 4 + (int) key.charAt(i);
g = h & 0xF0000000L;
if (g != 0)
h ^= g >>> 24;
h &= ~g;
}
return (int) (h % hsize);
}
private HashElem table[];
private int hsize;
private StackElem stack;
private String marker;
private class StackElem {
public String key;
public StackElem next;
public StackElem(String k, StackElem n) {
key = k;
next = n;
}
}
private int numelements_;
private class HashElem {
public Object data;
public String key;
public HashElem next;
public HashElem(String k, Object d, HashElem n) {
data = d;
key = k;
next = n;
}
}
}