2015-07-28 17:15:53 +03:00
|
|
|
from __future__ import unicode_literals
|
2015-10-10 07:54:55 +03:00
|
|
|
import codecs
|
2014-12-19 22:42:01 +03:00
|
|
|
|
|
|
|
from libc.string cimport memcpy
|
2015-01-12 02:26:22 +03:00
|
|
|
from murmurhash.mrmr cimport hash64
|
2014-12-19 22:42:01 +03:00
|
|
|
|
2015-07-28 15:45:37 +03:00
|
|
|
from cpython cimport PyUnicode_AS_DATA
|
|
|
|
from cpython cimport PyUnicode_GET_DATA_SIZE
|
|
|
|
|
2015-07-18 23:39:57 +03:00
|
|
|
from libc.stdint cimport int64_t
|
2014-12-19 22:42:01 +03:00
|
|
|
|
2015-07-18 23:39:57 +03:00
|
|
|
|
|
|
|
from .typedefs cimport hash_t, attr_t
|
2014-12-19 22:42:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
SEPARATOR = '\n|-SEP-|\n'
|
|
|
|
|
|
|
|
|
2015-01-12 02:26:22 +03:00
|
|
|
cpdef hash_t hash_string(unicode string) except 0:
|
2015-07-28 15:45:37 +03:00
|
|
|
# This has to be like this for
|
|
|
|
chars = <char*>PyUnicode_AS_DATA(string)
|
|
|
|
size = PyUnicode_GET_DATA_SIZE(string)
|
|
|
|
return hash64(chars, size, 1)
|
2015-01-12 02:26:22 +03:00
|
|
|
|
|
|
|
|
2015-07-20 12:26:46 +03:00
|
|
|
cdef unicode _decode(const Utf8Str* string):
|
2015-07-20 13:05:23 +03:00
|
|
|
cdef int i, length
|
2015-07-20 12:26:46 +03:00
|
|
|
if string.s[0] < sizeof(string.s) and string.s[0] != 0:
|
|
|
|
return string.s[1:string.s[0]+1].decode('utf8')
|
2015-07-20 13:05:23 +03:00
|
|
|
elif string.p[0] < 255:
|
2015-07-20 12:26:46 +03:00
|
|
|
return string.p[1:string.p[0]+1].decode('utf8')
|
|
|
|
else:
|
2015-07-20 13:05:23 +03:00
|
|
|
i = 0
|
|
|
|
length = 0
|
|
|
|
while string.p[i] == 255:
|
|
|
|
i += 1
|
|
|
|
length += 255
|
|
|
|
length += string.p[i]
|
2015-07-20 12:26:46 +03:00
|
|
|
i += 1
|
2015-07-20 13:05:23 +03:00
|
|
|
return string.p[i:length + i].decode('utf8')
|
2015-07-20 12:26:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef Utf8Str _allocate(Pool mem, const unsigned char* chars, int length) except *:
|
2015-07-20 13:05:23 +03:00
|
|
|
cdef int n_length_bytes
|
|
|
|
cdef int i
|
2015-07-20 12:26:46 +03:00
|
|
|
cdef Utf8Str string
|
|
|
|
assert length != 0
|
|
|
|
if length < sizeof(string.s):
|
|
|
|
string.s[0] = <unsigned char>length
|
|
|
|
memcpy(&string.s[1], chars, length)
|
|
|
|
return string
|
2015-07-20 13:05:23 +03:00
|
|
|
elif length < 255:
|
2015-07-20 12:26:46 +03:00
|
|
|
string.p = <unsigned char*>mem.alloc(length + 1, sizeof(unsigned char))
|
|
|
|
string.p[0] = length
|
|
|
|
memcpy(&string.p[1], chars, length)
|
|
|
|
assert string.s[0] >= sizeof(string.s) or string.s[0] == 0, string.s[0]
|
|
|
|
return string
|
|
|
|
else:
|
2015-07-20 13:05:23 +03:00
|
|
|
i = 0
|
|
|
|
n_length_bytes = (length // 255) + 1
|
|
|
|
string.p = <unsigned char*>mem.alloc(length + n_length_bytes, sizeof(unsigned char))
|
|
|
|
for i in range(n_length_bytes-1):
|
|
|
|
string.p[i] = 255
|
|
|
|
string.p[n_length_bytes-1] = length % 255
|
|
|
|
memcpy(&string.p[n_length_bytes], chars, length)
|
|
|
|
assert string.s[0] >= sizeof(string.s) or string.s[0] == 0, string.s[0]
|
|
|
|
return string
|
2014-12-21 23:25:43 +03:00
|
|
|
|
|
|
|
|
2014-12-19 22:42:01 +03:00
|
|
|
cdef class StringStore:
|
2015-01-24 12:49:15 +03:00
|
|
|
'''Map strings to and from integer IDs.'''
|
2014-12-19 22:42:01 +03:00
|
|
|
def __init__(self):
|
|
|
|
self.mem = Pool()
|
|
|
|
self._map = PreshMap()
|
|
|
|
self._resize_at = 10000
|
2015-07-20 12:26:46 +03:00
|
|
|
self.c = <Utf8Str*>self.mem.alloc(self._resize_at, sizeof(Utf8Str))
|
2015-01-14 16:33:16 +03:00
|
|
|
self.size = 1
|
2014-12-19 22:42:01 +03:00
|
|
|
|
|
|
|
property size:
|
|
|
|
def __get__(self):
|
2015-07-20 12:26:46 +03:00
|
|
|
return self.size -1
|
2014-12-19 22:42:01 +03:00
|
|
|
|
2015-06-23 01:02:50 +03:00
|
|
|
def __len__(self):
|
2015-07-20 12:26:46 +03:00
|
|
|
return self.size-1
|
2015-06-23 01:02:50 +03:00
|
|
|
|
2014-12-19 22:42:01 +03:00
|
|
|
def __getitem__(self, object string_or_id):
|
|
|
|
cdef bytes byte_string
|
|
|
|
cdef const Utf8Str* utf8str
|
2015-07-16 20:29:02 +03:00
|
|
|
cdef int id_
|
2014-12-19 22:42:01 +03:00
|
|
|
if isinstance(string_or_id, int) or isinstance(string_or_id, long):
|
2015-01-14 16:33:16 +03:00
|
|
|
if string_or_id == 0:
|
|
|
|
return u''
|
|
|
|
elif string_or_id < 1 or string_or_id >= self.size:
|
2014-12-19 22:42:01 +03:00
|
|
|
raise IndexError(string_or_id)
|
2015-07-20 12:26:46 +03:00
|
|
|
utf8str = &self.c[<int>string_or_id]
|
|
|
|
return _decode(utf8str)
|
2014-12-19 22:42:01 +03:00
|
|
|
elif isinstance(string_or_id, bytes):
|
2015-07-20 13:05:23 +03:00
|
|
|
if len(string_or_id) == 0:
|
|
|
|
return 0
|
2015-07-20 12:26:46 +03:00
|
|
|
utf8str = self.intern(<unsigned char*>string_or_id, len(string_or_id))
|
|
|
|
return utf8str - self.c
|
2014-12-19 22:42:01 +03:00
|
|
|
elif isinstance(string_or_id, unicode):
|
2015-07-20 13:05:23 +03:00
|
|
|
if len(string_or_id) == 0:
|
|
|
|
return 0
|
2014-12-19 22:42:01 +03:00
|
|
|
byte_string = string_or_id.encode('utf8')
|
2015-07-20 12:26:46 +03:00
|
|
|
utf8str = self.intern(<unsigned char*>byte_string, len(byte_string))
|
|
|
|
return utf8str - self.c
|
2014-12-19 22:42:01 +03:00
|
|
|
else:
|
|
|
|
raise TypeError(type(string_or_id))
|
|
|
|
|
2015-08-22 23:04:34 +03:00
|
|
|
def __iter__(self):
|
|
|
|
cdef int i
|
|
|
|
for i in range(self.size):
|
|
|
|
yield self[i]
|
|
|
|
|
2015-07-20 12:26:46 +03:00
|
|
|
cdef const Utf8Str* intern(self, unsigned char* chars, int length) except NULL:
|
|
|
|
# 0 means missing, but we don't bother offsetting the index.
|
|
|
|
key = hash64(chars, length * sizeof(char), 0)
|
|
|
|
value = <Utf8Str*>self._map.get(key)
|
|
|
|
if value != NULL:
|
|
|
|
return value
|
|
|
|
|
|
|
|
if self.size == self._resize_at:
|
|
|
|
self._realloc()
|
|
|
|
self.c[self.size] = _allocate(self.mem, chars, length)
|
|
|
|
self._map.set(key, <void*>&self.c[self.size])
|
|
|
|
self.size += 1
|
|
|
|
return &self.c[self.size-1]
|
2014-12-19 22:42:01 +03:00
|
|
|
|
|
|
|
def dump(self, loc):
|
|
|
|
cdef Utf8Str* string
|
2015-07-20 12:26:46 +03:00
|
|
|
cdef unicode py_string
|
2015-07-16 18:34:32 +03:00
|
|
|
cdef int i
|
2015-10-10 07:54:55 +03:00
|
|
|
with codecs.open(loc, 'w', 'utf8') as file_:
|
2015-07-20 12:26:46 +03:00
|
|
|
for i in range(1, self.size):
|
|
|
|
string = &self.c[i]
|
|
|
|
py_string = _decode(string)
|
2015-07-20 14:52:56 +03:00
|
|
|
file_.write(py_string)
|
2015-07-17 15:49:42 +03:00
|
|
|
if (i+1) != self.size:
|
|
|
|
file_.write(SEPARATOR)
|
2014-12-19 22:42:01 +03:00
|
|
|
|
|
|
|
def load(self, loc):
|
2015-10-10 07:54:55 +03:00
|
|
|
with codecs.open(loc, 'r', 'utf8') as file_:
|
2014-12-19 22:42:01 +03:00
|
|
|
strings = file_.read().split(SEPARATOR)
|
2015-08-23 21:49:18 +03:00
|
|
|
if strings == ['']:
|
|
|
|
return None
|
2014-12-19 22:42:01 +03:00
|
|
|
cdef unicode string
|
|
|
|
cdef bytes byte_string
|
2015-07-20 12:26:46 +03:00
|
|
|
for string in strings:
|
2014-12-19 22:42:01 +03:00
|
|
|
byte_string = string.encode('utf8')
|
2015-07-20 12:26:46 +03:00
|
|
|
self.intern(byte_string, len(byte_string))
|
|
|
|
|
|
|
|
def _realloc(self):
|
|
|
|
# We want to map straight to pointers, but they'll be invalidated if
|
|
|
|
# we resize our array. So, first we remap to indices, then we resize,
|
|
|
|
# then we can acquire the new pointers.
|
|
|
|
cdef Pool tmp_mem = Pool()
|
|
|
|
keys = <hash_t*>tmp_mem.alloc(self.size, sizeof(hash_t))
|
|
|
|
cdef hash_t key
|
|
|
|
cdef size_t addr
|
|
|
|
cdef const Utf8Str ptr
|
|
|
|
cdef size_t i
|
|
|
|
for key, addr in self._map.items():
|
|
|
|
# Find array index with pointer arithmetic
|
|
|
|
i = (<Utf8Str*>addr) - self.c
|
|
|
|
keys[i] = key
|
|
|
|
|
|
|
|
self._resize_at *= 2
|
|
|
|
cdef size_t new_size = self._resize_at * sizeof(Utf8Str)
|
|
|
|
self.c = <Utf8Str*>self.mem.realloc(self.c, new_size)
|
|
|
|
|
|
|
|
self._map = PreshMap(self.size)
|
|
|
|
for i in range(self.size):
|
|
|
|
self._map.set(keys[i], &self.c[i])
|