summaryrefslogtreecommitdiffstats
path: root/labb8/lib/StanfordCPPLib/hashmap.cpp
blob: 4dd77d64034d83d57d7d59d72741298736cf3f1a (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * File: hashmap.cpp
 * -----------------
 * This file contains the hash functions that are used in conjunction
 * with the HashMap class.
 */

#include <iostream>
#include <string>
#include "hashmap.h"
#include "hashset.h"
#include "lexicon.h"
#include "queue.h"
#include "set.h"
#include "stack.h"
#include "vector.h"
using namespace std;

/*
 * Implementation notes: hashCode
 * ------------------------------
 * This function takes a string key and uses it to derive a hash code,
 * which is a nonnegative integer related to the key by a deterministic
 * function that distributes keys well across the space of integers.
 * The general method is called linear congruence, which is also used
 * in random-number generators.  The specific algorithm used here is
 * called djb2 after the initials of its inventor, Daniel J. Bernstein,
 * Professor of Mathematics at the University of Illinois at Chicago.
 */

const int HASH_SEED = 5381;               /* Starting point for first cycle */
const int HASH_MULTIPLIER = 33;           /* Multiplier for each cycle      */
const int HASH_MASK = unsigned(-1) >> 1;  /* All 1 bits except the sign     */

int hashCode(const string & str) {
   unsigned hash = HASH_SEED;
   int n = str.length();
   for (int i = 0; i < n; i++) {
      hash = HASH_MULTIPLIER * hash + str[i];
   }
   return int(hash & HASH_MASK);
}

int hashCode(int key) {
   return key & HASH_MASK;
}

int hashCode(char key) {
   return key;
}

int hashCode(long key) {
   return int(key) & HASH_MASK;
}

int hashCode(double key) {
    char* byte = (char*) &key;
    unsigned hash = HASH_SEED;
    for (int i = 0; i < (int) sizeof(double); i++) {
        hash = HASH_MULTIPLIER * hash + (int) *byte++;
    }
    return hash & HASH_MASK;
}


// hashCode functions for various collections;
// added by Marty Stepp to allow compound collections.
// I'm a bit ashamed to have to rewrite so many prototypes, one for each
// element type; but I can't get it to compile with a template.
int hashCode(const HashSet<int>& s) {
    int code = HASH_SEED;
    foreach (int n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const HashSet<double>& s) {
    int code = HASH_SEED;
    foreach (double n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const HashSet<char>& s) {
    int code = HASH_SEED;
    foreach (char n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const HashSet<long>& s) {
    int code = HASH_SEED;
    foreach (long n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const HashSet<std::string>& s) {
    int code = HASH_SEED;
    foreach (std::string n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Lexicon& l) {
    int code = HASH_SEED;
    foreach (std::string n in l) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Queue<int>& q) {
    int code = HASH_SEED;
    Queue<int> backup = q;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.dequeue());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Queue<double>& q) {
    int code = HASH_SEED;
    Queue<double> backup = q;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.dequeue());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Queue<char>& q) {
    int code = HASH_SEED;
    Queue<char> backup = q;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.dequeue());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Queue<long>& q) {
    int code = HASH_SEED;
    Queue<long> backup = q;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.dequeue());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Queue<std::string>& q) {
    int code = HASH_SEED;
    Queue<std::string> backup = q;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.dequeue());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Set<int>& s) {
    int code = HASH_SEED;
    foreach (int n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Set<double>& s) {
    int code = HASH_SEED;
    foreach (double n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Set<char>& s) {
    int code = HASH_SEED;
    foreach (char n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Set<long>& s) {
    int code = HASH_SEED;
    foreach (long n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Set<std::string>& s) {
    int code = HASH_SEED;
    foreach (std::string n in s) {
        code = HASH_MULTIPLIER * code + hashCode(n);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Stack<int>& s) {
    int code = HASH_SEED;
    Stack<int> backup = s;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.pop());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Stack<double>& s) {
    int code = HASH_SEED;
    Stack<double> backup = s;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.pop());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Stack<char>& s) {
    int code = HASH_SEED;
    Stack<char> backup = s;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.pop());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Stack<long>& s) {
    int code = HASH_SEED;
    Stack<long> backup = s;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.pop());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Stack<std::string>& s) {
    int code = HASH_SEED;
    Stack<std::string> backup = s;
    while (!backup.isEmpty()) {
        code = HASH_MULTIPLIER * code + hashCode(backup.pop());
    }
    return int(code & HASH_MASK);
}

int hashCode(const Vector<int>& v) {
    int code = HASH_SEED;
    for (int i = 0, size = v.size(); i < size; i++) {
        code = HASH_MULTIPLIER * code + hashCode(v[i]);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Vector<double>& v) {
    int code = HASH_SEED;
    for (int i = 0, size = v.size(); i < size; i++) {
        code = HASH_MULTIPLIER * code + hashCode(v[i]);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Vector<char>& v) {
    int code = HASH_SEED;
    for (int i = 0, size = v.size(); i < size; i++) {
        code = HASH_MULTIPLIER * code + hashCode(v[i]);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Vector<long>& v) {
    int code = HASH_SEED;
    for (int i = 0, size = v.size(); i < size; i++) {
        code = HASH_MULTIPLIER * code + hashCode(v[i]);
    }
    return int(code & HASH_MASK);
}

int hashCode(const Vector<std::string>& v) {
    int code = HASH_SEED;
    for (int i = 0, size = v.size(); i < size; i++) {
        code = HASH_MULTIPLIER * code + hashCode(v[i]);
    }
    return int(code & HASH_MASK);
}

//template <typename ValueType>
//int hashCode(const Vector<ValueType>& v) {
//    int code = 0;
//    for (int i = 0, size = v.size(); i < size; i++) {
//        code = 31 * code + hashCode(v[i]);
//    }
//    return code;
//}