-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhattrie.pyx
117 lines (88 loc) · 2.93 KB
/
hattrie.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
# distutils: language=c++
from cpython.ref cimport PyObject
from cython.operator cimport dereference as deref
from cython.operator cimport preincrement as inc
from htrie_map cimport htrie_map
from libcpp cimport bool as cbool
from libcpp.string cimport string
from smartptr cimport PyObjectSmartPtr
ctypedef PyObject *c_value_t
ctypedef object p_value_t
cdef class HatTrieMap:
cdef htrie_map[char, PyObjectSmartPtr] hattrie
def __init__(self):
pass
cpdef cbool empty(self):
return self.hattrie.empty()
cpdef size_t size(self):
return self.hattrie.size()
cpdef size_t max_size(self):
return self.hattrie.max_size()
cpdef size_t max_key_size(self):
return self.hattrie.max_key_size()
cpdef void shrink_to_fit(self):
self.hattrie.shrink_to_fit()
cpdef void clear(self):
self.hattrie.clear()
def __getitem__(self, string key):
try:
return <p_value_t>self.hattrie.at(key).get()
except IndexError:
raise KeyError(key)
def __setitem__(self, string key, object value):
self.hattrie.insert(key, PyObjectSmartPtr(<c_value_t>value))
def __delitem__(self, string key):
cdef size_t num = self.hattrie.erase(key)
if num == 0:
raise KeyError(key)
def __contains__(self, string key):
try:
self.hattrie.at(key)
return True
except IndexError:
return False
def keys(self):
cdef htrie_map[char, PyObjectSmartPtr].const_iterator it = self.hattrie.const_begin()
while it != self.hattrie.cend():
yield it.key()
inc(it)
def values(self):
cdef htrie_map[char, PyObjectSmartPtr].const_iterator it = self.hattrie.const_begin()
while it != self.hattrie.cend():
yield <p_value_t>it.value().get()
inc(it)
def items(self):
cdef htrie_map[char, PyObjectSmartPtr].const_iterator it = self.hattrie.const_begin()
while it != self.hattrie.cend():
yield (it.key(), <p_value_t>it.value().get())
inc(it)
def __iter__(self):
return self.keys()
def pop(self, string key):
raise NotImplementedError
cpdef cbool insert(self, string key, object value):
return self.hattrie.insert(key, PyObjectSmartPtr(<c_value_t>value)).second
def update(self, map):
for k, v in map.items():
self.hattrie.insert(<string>k, PyObjectSmartPtr(<c_value_t>v))
cpdef size_t count(self, string key):
return self.hattrie.count(key)
def longest_prefix(self, string key):
cdef htrie_map[char, PyObjectSmartPtr].const_iterator it = self.hattrie.const_longest_prefix(key)
while it != self.hattrie.cend():
yield (it.key(), <p_value_t>it.value().get())
inc(it)
cpdef size_t erase_prefix(self, string prefix):
return self.hattrie.erase_prefix(prefix)
@property
def max_load_factor(self):
return self.hattrie.max_load_factor()
@max_load_factor.setter
def max_load_factor(self, float ml):
self.hattrie.max_load_factor(ml)
@property
def burst_threshold(self):
return self.hattrie.burst_threshold()
@burst_threshold.setter
def burst_threshold(self, size_t threshold):
self.hattrie.burst_threshold(threshold)