2014-09-13 05:14:43 +04:00
|
|
|
# cython: profile=True
|
2014-09-18 01:09:24 +04:00
|
|
|
from cymem.cymem cimport Address
|
2014-09-13 05:14:43 +04:00
|
|
|
cimport cython
|
|
|
|
|
|
|
|
|
2014-09-13 04:02:41 +04:00
|
|
|
cdef class PointerHash:
|
|
|
|
def __cinit__(self, size_t initial_size=8):
|
|
|
|
self.size = initial_size
|
|
|
|
self.filled = 0
|
|
|
|
# Size must be power of two
|
2014-09-15 07:03:44 +04:00
|
|
|
assert self.size != 0
|
2014-09-13 04:02:41 +04:00
|
|
|
assert self.size & (self.size - 1) == 0
|
2014-09-17 22:02:26 +04:00
|
|
|
self._mem = Address(self.size, sizeof(Cell))
|
|
|
|
self.cells = <Cell*>self._mem.addr
|
2014-09-13 04:02:41 +04:00
|
|
|
|
|
|
|
def __getitem__(self, key_t key):
|
2014-09-13 05:14:43 +04:00
|
|
|
assert key != 0
|
2014-09-13 19:02:06 +04:00
|
|
|
cdef val_t value = self.get(key)
|
|
|
|
return <size_t>value if value != NULL else None
|
2014-09-13 04:02:41 +04:00
|
|
|
|
2014-09-13 19:02:06 +04:00
|
|
|
def __setitem__(self, key_t key, size_t value):
|
|
|
|
assert key != 0 and value != 0
|
|
|
|
self.set(key, <val_t>value)
|
2014-09-13 04:02:41 +04:00
|
|
|
|
2014-09-15 07:03:44 +04:00
|
|
|
cdef val_t get(self, key_t key) nogil:
|
|
|
|
cdef Cell* cell = _find_cell(self.cells, self.size, key)
|
2014-09-13 18:43:42 +04:00
|
|
|
return cell.value
|
2014-09-13 04:02:41 +04:00
|
|
|
|
2014-09-13 19:02:06 +04:00
|
|
|
cdef void set(self, key_t key, val_t value) except *:
|
2014-09-13 18:43:42 +04:00
|
|
|
cdef Cell* cell
|
2014-09-15 07:03:44 +04:00
|
|
|
cell = _find_cell(self.cells, self.size, key)
|
2014-09-13 18:43:42 +04:00
|
|
|
if cell.key == 0:
|
|
|
|
cell.key = key
|
2014-09-13 04:02:41 +04:00
|
|
|
self.filled += 1
|
2014-09-13 18:43:42 +04:00
|
|
|
cell.value = value
|
2014-09-13 04:02:41 +04:00
|
|
|
if (self.filled + 1) * 4 >= (self.size * 3):
|
|
|
|
self.resize(self.size * 2)
|
|
|
|
|
2014-09-13 18:43:42 +04:00
|
|
|
cdef void resize(self, size_t new_size) except *:
|
2014-09-15 07:03:44 +04:00
|
|
|
assert new_size != 0
|
2014-09-13 05:14:43 +04:00
|
|
|
assert (new_size & (new_size - 1)) == 0 # Must be a power of 2
|
2014-09-13 04:02:41 +04:00
|
|
|
assert self.filled * 4 <= new_size * 3
|
|
|
|
|
|
|
|
cdef Cell* old_cells = self.cells
|
|
|
|
cdef size_t old_size = self.size
|
|
|
|
|
|
|
|
self.size = new_size
|
2014-09-17 22:02:26 +04:00
|
|
|
cdef Address new_mem = Address(new_size, sizeof(Cell))
|
|
|
|
self.cells = <Cell*>new_mem.addr
|
2014-09-13 05:14:43 +04:00
|
|
|
|
|
|
|
self.filled = 0
|
|
|
|
cdef size_t i
|
2014-09-13 18:43:42 +04:00
|
|
|
cdef size_t slot
|
2014-09-13 04:02:41 +04:00
|
|
|
for i in range(old_size):
|
2014-09-13 18:43:42 +04:00
|
|
|
if old_cells[i].key != 0:
|
2014-09-13 19:02:06 +04:00
|
|
|
assert old_cells[i].value != NULL, i
|
|
|
|
self.set(old_cells[i].key, old_cells[i].value)
|
2014-09-17 22:02:26 +04:00
|
|
|
self._mem = new_mem
|
2014-09-13 18:43:42 +04:00
|
|
|
|
|
|
|
|
|
|
|
@cython.cdivision
|
|
|
|
cdef inline Cell* _find_cell(Cell* cells, size_t size, key_t key) nogil:
|
2014-09-15 07:03:44 +04:00
|
|
|
# Modulo for powers-of-two via bitwise &
|
|
|
|
cdef size_t i = (key & (size - 1))
|
2014-09-13 18:43:42 +04:00
|
|
|
while cells[i].key != 0 and cells[i].key != key:
|
2014-09-15 07:03:44 +04:00
|
|
|
i = (i + 1) & (size - 1)
|
2014-09-13 18:43:42 +04:00
|
|
|
return &cells[i]
|