-
Notifications
You must be signed in to change notification settings - Fork 0
/
ht_openaddr.pyx
191 lines (152 loc) · 4.14 KB
/
ht_openaddr.pyx
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
from libc.stdlib cimport malloc, free, calloc, rand
from array_c cimport array_c, create_arr, free_arr, push_back_arr, isin_arr, count_arr, py2arr, print_array
import cython
from cython.parallel import prange
from time import time
cdef hashtable* make_hashtable(size_t n):
cdef:
hashtable * h = <hashtable*>malloc(sizeof(hashtable))
h.items = <size_t*>calloc(n + 1, sizeof(size_t)) # zero-terminated membuffer
h.capacity = n
h.size = 0
return h
cdef void free_hashtable(hashtable* h):
free(h.items)
free(h)
cdef void print_ht(hashtable* h):
print("=== size:", h.size)
for i in range(h.capacity):
print(f"[{i}]: ", h.items[i])
cdef size_t twosum(hashtable* h, long long int t) nogil:
"""
Test for integer pairs in hashtable, such that x + y = t
"""
cdef:
size_t i, j
size_t cnt = 0
long long int x
for i in range(h.capacity):
x = h.items[i]
if x:
if search(h, t - x):
return 1
return 0
def test_twosum_c(verbose=True):
if verbose:
print()
start = time()
with open("2sum.txt", "r") as f:
py_list = [int(s) for s in f.readlines()]
cdef:
long int i
size_t cnt = 0
long long int x, t
hashtable* h = make_hashtable(2002823)
if verbose:
print(f"read_file: {time() - start:.2f}s")
start = time()
for i in range(len(py_list)):
x = <long long int>py_list[i]
if not search(h, x):
insert(h, x)
if verbose:
print(f"make_hashtable: {time() - start:.2f}s")
start = time()
# for i in prange(20001, nogil=True):
# t = -10000 + i
# cnt += twosum(h, t)
for i in range(64):
t = -10000 + i
cnt += twosum(h, t)
if verbose:
print(f"twosum: {time() - start:.2f}s")
free_hashtable(h)
return cnt
""" ################################################################ """
""" ######################### UNIT TESTS ########################### """
""" ################################################################ """
import numpy as np
def test_insert():
cdef:
size_t x = rand()
hashtable* h = make_hashtable(7)
insert(h, x)
assert h.size == 1
assert search(h, x)
assert not search(h, x + 1)
free_hashtable(h)
def test_search_rnd():
cdef:
size_t i
# size_t n = 102100
size_t n = 50000
hashtable* h = make_hashtable(102197)
long long int [:] arr = np.random.randint(-100000, 100000, n, dtype=np.int64)
for i in range(n):
insert(h, arr[i])
for i in range(n):
assert search(h, arr[i])
free_hashtable(h)
def test_delete_single():
cdef:
size_t i
size_t x = rand()
hashtable* h = make_hashtable(7)
insert(h, x)
delete(h, x)
assert not search(h, x)
assert h.size == 0
for i in range(h.capacity):
assert h.items[i] == 0
free_hashtable(h)
def test_delete_collision():
cdef:
size_t i
hashtable* h = make_hashtable(5)
insert(h, 1)
insert(h, 6)
insert(h, 2)
insert(h, 1)
# print()
# print_ht(h)
delete(h, 1)
# print_ht(h)
delete(h, 6)
# print_ht(h)
delete(h, 1)
# print_ht(h)
delete(h, 2)
# print_ht(h)
assert h.size == 0
free_hashtable(h)
def test_delete_rnd():
cdef:
size_t i
size_t n = 90000
hashtable* h = make_hashtable(102197)
long long int [:] arr = np.random.randint(-100000, 100000, n, dtype=np.int64)
for i in range(n):
insert(h, arr[i])
# print()
# print("h.size", h.size)
for i in range(n):
delete(h, arr[i])
for i in range(n):
assert not search(h, arr[i])
for i in range(h.capacity):
assert h.items[i] == 0
# print("h.size", h.size)
assert h.size == 0
def test_twosum():
cdef:
size_t i
hashtable* h = make_hashtable(7)
insert(h, 1)
insert(h, -1)
assert twosum(h, 0) == 1
insert(h, -2)
insert(h, 2)
assert twosum(h, 0) == 1
insert(h, 3)
assert twosum(h, 4) == 1
free_hashtable(h)