-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.py
executable file
·107 lines (90 loc) · 3.89 KB
/
HashTable.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
from MoveWall import *
class HashObject:
def __init__(self, isBall, number):
self.isBall = isBall
self.number = number
class Pair:
def __init__(self, i, j):
self.i = i
self.j = j
class HashTable:
def __init__(self, balls):
wall = MoveWall.getInstance()
self.width = wall.width
self.height = wall.height
radius = 0
for ball in balls:
if ball.radius > radius:
radius = ball.radius
self.delta = 3 * radius
self.elementsOfX = int(ceil(self.width / self.delta))
self.elementsOfY = int(ceil(self.height / self.delta))
self.numOfElements = self.elementsOfX * self.elementsOfY
self.table = [[] * 1 for _ in range(self.numOfElements)]
def hashBall(self, ball):
elementOfX = int(floor(ball.x / self.delta))
elementOfY = int(floor(ball.y / self.delta))
resultElement = (self.elementsOfX * elementOfY) + elementOfX
return resultElement
def update(self, balls):
for i in range(self.numOfElements):
self.table[i].clear()
for i in range(len(balls)):
index = self.hashBall(balls[i])
if index >= len(self.table):
print('Out of range', 'number', i, balls[i].x, balls[i].y, balls[i].color, balls[i].radius)
else:
self.table[index].append(HashObject(isBall=True, number=i))
def getPairs(self, balls):
self.update(balls)
pairs = []
for i in range(self.numOfElements):
isDiagonalRight = True
isDiagonalLeft = True
elements1 = self.table[i]
# -------- Right block --------------
for j in range(len(elements1)):
for k in range(j + 1, len(elements1)):
pairs.append(Pair(elements1[j], elements1[k]))
# -----------------------------------
if (i % self.elementsOfX) != (self.elementsOfX - 1):
elements2 = self.table[i + 1]
for j in range(len(elements1)):
for k in range(len(elements2)):
pairs.append(Pair(elements1[j], elements2[k]))
else:
isDiagonalRight = False
if floor(i / self.elementsOfX) != (self.elementsOfY - 1):
elements3 = self.table[i + self.elementsOfX]
for j in range(len(elements1)):
for k in range(len(elements3)):
pairs.append(Pair(elements1[j], elements3[k]))
else:
isDiagonalRight = False
isDiagonalLeft = False
if isDiagonalRight:
elements4 = self.table[i + self.elementsOfX + 1]
for j in range(len(elements1)):
for k in range(len(elements4)):
pairs.append(Pair(elements1[j], elements4[k]))
if isDiagonalLeft and ((i % self.elementsOfX) != 0):
elements5 = self.table[i + self.elementsOfX - 1]
for j in range(len(elements1)):
for k in range(len(elements5)):
pairs.append(Pair(elements1[j], elements5[k]))
return pairs
# def updateDelta(self, balls):
# radius = 0
# for ball in balls:
# if ball.radius > radius:
# radius = ball.radius
# self.delta = 3 * radius
# self.elementsOfX = int(ceil(self.width / self.delta))
# self.elementsOfY = int(ceil(self.height / self.delta))
# self.numOfElements = self.elementsOfX * self.elementsOfY
# self.table = [[] * 1 for _ in range(self.numOfElements)]
# print('new delta ', self.delta, '\n',
# 'new elements of x', self.elementsOfX, '\n',
# 'new elements of y', self.elementsOfY, '\n',
# 'new elements num', self.numOfElements, '\n\n')
#