-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitHash.py
More file actions
66 lines (52 loc) · 2.79 KB
/
Copy pathBitHash.py
File metadata and controls
66 lines (52 loc) · 2.79 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
import cityhash
import random
__rnd = random.Random() # get a random number generator for this module
__rnd.seed("BitHash random numbers") # set the RNG seed to a known value
__BitHashSeeds = None
__MAX_SEEDS = 1000
# this function causes subsequent calls to BitHash to be based on new random
# seeds. This is useful in the event that client code needs a new hash
# function, for example, for Cuckoo Hashing.
def ResetBitHash():
global __BitHashSeeds
if not __BitHashSeeds: __BitHashSeeds = [0] * __MAX_SEEDS
for i in range(__MAX_SEEDS):
__BitHashSeeds[i] = __rnd.getrandbits(64)
ResetBitHash() # set up the seeds in advance of the first BitHash call
# returns a 64-bit hash value using Google's very fast and robust
# CityHash. You can simulate having having many independent
# hash functions by specifying which hashFuncNum you want.This
# causes a different seed to be used for each possible hashFuncNum.
def BitHash(s, hashFuncNum = 1):
return cityhash.CityHash64WithSeed(str(s), __BitHashSeeds[hashFuncNum-1])
def __main():
# use BitHash to get two hash values for each of a bunch of strings
# and print them out.
v1 = BitHash("foo"); v2 = BitHash("foo", 2); print(hex(v1), hex(v2))
v1 = BitHash("bar"); v2 = BitHash("bar", 2); print(hex(v1), hex(v2))
v1 = BitHash("baz"); v2 = BitHash("baz", 2); print(hex(v1), hex(v2))
v1 = BitHash("blat"); v2 = BitHash("blat",2); print(hex(v1), hex(v2))
# now reset BitHash so that it is effectively a new set of hash functions,
# and print out the hash values for the same words.
print("\nresetting BitHash to a new set of hash functions\n")
ResetBitHash()
v1 = BitHash("foo"); v2 = BitHash("foo", 2); print(hex(v1), hex(v2))
v1 = BitHash("bar"); v2 = BitHash("bar", 2); print(hex(v1), hex(v2))
v1 = BitHash("baz"); v2 = BitHash("baz", 2); print(hex(v1), hex(v2))
v1 = BitHash("blat"); v2 = BitHash("blat",2); print(hex(v1), hex(v2))
# now reset BitHash again so that it is effectively yet another set of
# hash functiona, and print out the hash values for the same words.
print("\nresetting BitHash to yet another set of hash functions\n")
ResetBitHash()
v1 = BitHash("foo"); v2 = BitHash("foo", 3); print(hex(v1), hex(v2))
v1 = BitHash("bar"); v2 = BitHash("bar", 3); print(hex(v1), hex(v2))
v1 = BitHash("baz"); v2 = BitHash("baz", 3); print(hex(v1), hex(v2))
v1 = BitHash("blat"); v2 = BitHash("blat",3); print(hex(v1), hex(v2))
def __main2():
numBuckets = int(input("How many buckets? "))
while True:
s = input("string to hash? ")
hashValue = BitHash(s) % numBuckets
print("Hash value:", hashValue)
if __name__ == '__main__':
__main2()