@@ -163,8 +163,9 @@ ctypedef fused vector_data:
163
163
Complex64VectorData
164
164
StringVectorData
165
165
166
- cdef bint needs_resize(vector_data *data) noexcept nogil:
167
- return data.size == data.capacity
166
+
167
+ cdef bint needs_resize(Py_ssize_t nelems, Py_ssize_t capacity) noexcept nogil:
168
+ return nelems >= capacity
168
169
169
170
# ----------------------------------------------------------------------
170
171
# Vector
@@ -214,8 +215,8 @@ cdef class {{name}}Vector(Vector):
214
215
self.ao = np.empty(self.data.capacity, dtype=np.{{dtype}})
215
216
self.data.data = <{{c_type}}*>self.ao.data
216
217
217
- cdef resize(self):
218
- self.data.capacity = max(self.data.capacity * 4 , _INIT_VEC_CAP)
218
+ cdef resize(self, Py_ssize_t new_size ):
219
+ self.data.capacity = max(new_size , _INIT_VEC_CAP)
219
220
self.ao.resize(self.data.capacity, refcheck=False)
220
221
self.data.data = <{{c_type}}*>self.ao.data
221
222
@@ -234,17 +235,28 @@ cdef class {{name}}Vector(Vector):
234
235
235
236
cdef void append(self, {{c_type}} x) noexcept:
236
237
237
- if needs_resize(& self.data):
238
+ if needs_resize(self.data.size, self.data.capacity ):
238
239
if self.external_view_exists:
239
240
raise ValueError("external reference but "
240
241
"Vector.resize() needed")
241
- self.resize()
242
+ self.resize(self.data.capacity * 4 )
242
243
243
244
append_data_{{dtype}}(&self.data, x)
244
245
245
246
cdef extend(self, const {{c_type}}[:] x):
246
- for i in range(len(x)):
247
- self.append(x[i])
247
+ cdef Py_ssize_t x_size = len(x)
248
+ if x_size == 0:
249
+ return
250
+
251
+ cdef Py_ssize_t needed_size = self.data.size + x_size
252
+ if needs_resize(needed_size, self.data.capacity):
253
+ if self.external_view_exists:
254
+ raise ValueError("external reference but "
255
+ "Vector.resize() needed")
256
+ self.resize(needed_size)
257
+
258
+ memcpy(self.data.data + self.data.size, &x[0], x_size * sizeof({{c_type}}))
259
+ self.data.size = needed_size
248
260
249
261
{{endfor}}
250
262
@@ -260,7 +272,7 @@ cdef class StringVector(Vector):
260
272
if self.data.data is NULL:
261
273
raise MemoryError()
262
274
263
- cdef resize(self):
275
+ cdef resize(self, Py_ssize_t new_size ):
264
276
cdef:
265
277
char **orig_data
266
278
Py_ssize_t i, orig_capacity
@@ -297,8 +309,8 @@ cdef class StringVector(Vector):
297
309
298
310
cdef void append(self, char *x) noexcept:
299
311
300
- if needs_resize(& self.data):
301
- self.resize()
312
+ if needs_resize(self.data.size, self.data.capacity ):
313
+ self.resize(self.data.capacity * 4 )
302
314
303
315
append_data_string(&self.data, x)
304
316
@@ -684,18 +696,18 @@ cdef class {{name}}HashTable(HashTable):
684
696
continue
685
697
686
698
seen_na = True
687
- if needs_resize(ud):
699
+ if needs_resize(ud.size, ud.capacity ):
688
700
with gil:
689
701
if uniques.external_view_exists:
690
702
raise ValueError("external reference to "
691
703
"uniques held, but "
692
704
"Vector.resize() needed")
693
- uniques.resize()
705
+ uniques.resize(uniques.data.capacity * 4 )
694
706
if result_mask.external_view_exists:
695
707
raise ValueError("external reference to "
696
708
"result_mask held, but "
697
709
"Vector.resize() needed")
698
- result_mask.resize()
710
+ result_mask.resize(result_mask.data.capacity * 4 )
699
711
append_data_{{dtype}}(ud, val)
700
712
append_data_uint8(rmd, 1)
701
713
continue
@@ -706,19 +718,19 @@ cdef class {{name}}HashTable(HashTable):
706
718
# k hasn't been seen yet
707
719
k = kh_put_{{dtype}}(self.table, val, &ret)
708
720
709
- if needs_resize(ud):
721
+ if needs_resize(ud.size, ud.capacity ):
710
722
with gil:
711
723
if uniques.external_view_exists:
712
724
raise ValueError("external reference to "
713
725
"uniques held, but "
714
726
"Vector.resize() needed")
715
- uniques.resize()
727
+ uniques.resize(uniques.data.capacity * 4 )
716
728
if use_result_mask:
717
729
if result_mask.external_view_exists:
718
730
raise ValueError("external reference to "
719
731
"result_mask held, but "
720
732
"Vector.resize() needed")
721
- result_mask.resize()
733
+ result_mask.resize(result_mask.data.capacity * 4 )
722
734
append_data_{{dtype}}(ud, val)
723
735
if use_result_mask:
724
736
append_data_uint8(rmd, 0)
@@ -849,9 +861,9 @@ cdef class {{name}}HashTable(HashTable):
849
861
k = kh_put_{{dtype}}(self.table, val, &ret)
850
862
self.table.vals[k] = count
851
863
852
- if needs_resize(ud):
864
+ if needs_resize(ud.size, ud.capacity ):
853
865
with gil:
854
- uniques.resize()
866
+ uniques.resize(uniques.data.capacity * 4 )
855
867
append_data_{{dtype}}(ud, val)
856
868
labels[i] = count
857
869
count += 1
0 commit comments