-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing.py
132 lines (106 loc) · 3.34 KB
/
hashing.py
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
import math
from numpy import array,seterr
import time
seterr(all='raise')
# Function that returns True if n
# is prime else returns False
def isPrime(n):
# Corner cases
if(n <= 1):
return False
if(n <= 3):
return True
# This is checked so that we can skip
# middle five numbers in below loop
if(n % 2 == 0 or n % 3 == 0):
return False
for i in range(5,int(math.sqrt(n) + 1), 6):
if(n % i == 0 or n % (i + 2) == 0):
return False
return True
# Function to return the smallest
# prime number greater than N
def nextPrime(N):
# Base case
if (N <= 1):
return 2
prime = N
found = False
# Loop continuously until isPrime returns
# True for a number greater than n
while(not found):
prime = prime + 1
if(isPrime(prime) == True):
found = True
return prime
class Hashing:
def __init__(self,cell_size,N,d={}):
self.d = d
self.cell_size = cell_size
self.table_size = N
self.p1 = 73856093
self.p2 = 19349669
self.p3 = 83492791
def r_c(self,point):
return [math.floor(point[0]/self.cell_size),
math.floor(point[1]/self.cell_size),
math.floor(point[2]/self.cell_size)]
def _hash(self,point):
r = array([point[0]*self.p1,
point[1]*self.p2,
point[2]*self.p3])
return (r[0]^r[1]^r[2]) % self.table_size
def _add(self,point,obj):
r_c = self.r_c(point)
hh = self._hash(r_c)
if hh not in self.d.keys():
self.d[hh] = list()
if obj not in self.d[hh]:
self.d[hh].append(obj)
for i in self.d:
if obj in self.d[i] and i!=hh:
for j in self.d[i]:
if j == obj:
ind = self.d[i].index(j)
del self.d[i][ind]
return
#self._del(point,obj)
def _del(self,point,obj):
i = self.d.get(self._hash(point))
for j in i:
if j == obj:
del i[j]
def possible_neighbors(self,point):
point = array(point)
L = []
min_point = point - self.cell_size
max_point = point + self.cell_size
BBmin = self.r_c(min_point)
BBmax = self.r_c(max_point)
x_count = BBmin[0]
while x_count <= BBmax[0]:
y_count = BBmin[1]
while y_count <= BBmax[1]:
z_count = BBmin[2]
while z_count <= BBmax[2]:
new_point = [x_count,y_count,z_count]
i = self.d.get(self._hash(new_point))
if i != None:
for j in i:
L.append(j)
z_count += 1
y_count += 1
x_count += 1
return L
# h = 10
# N = 1000
# #Hashing(h,N)._add([10,11,32])
# start = time.time()
# print(Hashing(h,N)._add([10,25,32],88))
# print(Hashing(h,N)._add([10,11,32],68))
# print(Hashing(h,N)._add([10,11,32],67))
# print(Hashing(h,N)._add([10,14,48],65))
# x = Hashing(h,N).possible_neighbors([10,24,32])
# print(x)
# print(len(x))
# print(start-time.time())