@@ -71,6 +71,7 @@ cdef class {{name}}Vector:
71
71
72
72
{{if dtype != 'int64'}}
73
73
cdef:
74
+ bint external_view_exists
74
75
{{name}}VectorData *data
75
76
ndarray ao
76
77
{{endif}}
@@ -80,14 +81,15 @@ cdef class {{name}}Vector:
80
81
sizeof({{name}}VectorData))
81
82
if not self.data:
82
83
raise MemoryError()
84
+ self.external_view_exists = False
83
85
self.data.n = 0
84
86
self.data.m = _INIT_VEC_CAP
85
87
self.ao = np.empty(self.data.m, dtype={{idtype}})
86
88
self.data.data = <{{arg}}*> self.ao.data
87
89
88
90
cdef resize(self):
89
91
self.data.m = max(self.data.m * 4, _INIT_VEC_CAP)
90
- self.ao.resize(self.data.m)
92
+ self.ao.resize(self.data.m, refcheck=False )
91
93
self.data.data = <{{arg}}*> self.ao.data
92
94
93
95
def __dealloc__(self):
@@ -99,13 +101,19 @@ cdef class {{name}}Vector:
99
101
return self.data.n
100
102
101
103
cpdef to_array(self):
102
- self.ao.resize(self.data.n)
104
+ if self.data.m != self.data.n:
105
+ if self.external_view_exists:
106
+ raise ValueError("should have raised on append(), m=%d n=%d" % (self.data.m, self.data.n))
107
+ self.ao.resize(self.data.n, refcheck=False)
103
108
self.data.m = self.data.n
109
+ self.external_view_exists = True
104
110
return self.ao
105
111
106
112
cdef inline void append(self, {{arg}} x):
107
113
108
114
if needs_resize(self.data):
115
+ if self.external_view_exists:
116
+ raise ValueError("external reference but Vector.resize() needed")
109
117
self.resize()
110
118
111
119
append_data_{{dtype}}(self.data, x)
@@ -120,15 +128,19 @@ cdef class StringVector:
120
128
121
129
cdef:
122
130
StringVectorData *data
131
+ bint external_view_exists
123
132
124
133
def __cinit__(self):
125
134
self.data = <StringVectorData *>PyMem_Malloc(
126
135
sizeof(StringVectorData))
127
136
if not self.data:
128
137
raise MemoryError()
138
+ self.external_view_exists = False
129
139
self.data.n = 0
130
140
self.data.m = _INIT_VEC_CAP
131
141
self.data.data = <char **> malloc(self.data.m * sizeof(char *))
142
+ if not self.data.data:
143
+ raise MemoryError()
132
144
133
145
cdef resize(self):
134
146
cdef:
@@ -138,9 +150,10 @@ cdef class StringVector:
138
150
m = self.data.m
139
151
self.data.m = max(self.data.m * 4, _INIT_VEC_CAP)
140
152
141
- # TODO: can resize?
142
153
orig_data = self.data.data
143
154
self.data.data = <char **> malloc(self.data.m * sizeof(char *))
155
+ if not self.data.data:
156
+ raise MemoryError()
144
157
for i in range(m):
145
158
self.data.data[i] = orig_data[i]
146
159
@@ -164,6 +177,7 @@ cdef class StringVector:
164
177
for i in range(self.data.n):
165
178
val = self.data.data[i]
166
179
ao[i] = val
180
+ self.external_view_exists = True
167
181
self.data.m = self.data.n
168
182
return ao
169
183
@@ -181,8 +195,10 @@ cdef class ObjectVector:
181
195
PyObject **data
182
196
size_t n, m
183
197
ndarray ao
198
+ bint external_view_exists
184
199
185
200
def __cinit__(self):
201
+ self.external_view_exists = False
186
202
self.n = 0
187
203
self.m = _INIT_VEC_CAP
188
204
self.ao = np.empty(_INIT_VEC_CAP, dtype=object)
@@ -193,17 +209,23 @@ cdef class ObjectVector:
193
209
194
210
cdef inline append(self, object o):
195
211
if self.n == self.m:
212
+ if self.external_view_exists:
213
+ raise ValueError("external reference but Vector.resize() needed")
196
214
self.m = max(self.m * 2, _INIT_VEC_CAP)
197
- self.ao.resize(self.m)
215
+ self.ao.resize(self.m, refcheck=False )
198
216
self.data = <PyObject**> self.ao.data
199
217
200
218
Py_INCREF(o)
201
219
self.data[self.n] = <PyObject*> o
202
220
self.n += 1
203
221
204
222
def to_array(self):
205
- self.ao.resize(self.n)
206
- self.m = self.n
223
+ if self.m != self.n:
224
+ if self.external_view_exists:
225
+ raise ValueError("should have raised on append()")
226
+ self.ao.resize(self.n, refcheck=False)
227
+ self.m = self.n
228
+ self.external_view_exists = True
207
229
return self.ao
208
230
209
231
@@ -362,6 +384,9 @@ cdef class {{name}}HashTable(HashTable):
362
384
363
385
if needs_resize(ud):
364
386
with gil:
387
+ if uniques.external_view_exists:
388
+ raise ValueError("external reference to uniques held, "
389
+ "but Vector.resize() needed")
365
390
uniques.resize()
366
391
append_data_{{dtype}}(ud, val)
367
392
labels[i] = count
0 commit comments