@@ -133,7 +133,7 @@ dtypes = [('Complex128', 'complex128', 'khcomplex128_t'),
133
133
134
134
ctypedef struct {{name}}VectorData:
135
135
{{c_type}} *data
136
- Py_ssize_t n, m
136
+ Py_ssize_t size, capacity
137
137
138
138
{{endif}}
139
139
@@ -143,8 +143,8 @@ ctypedef struct {{name}}VectorData:
143
143
cdef void append_data_{{dtype}}({{name}}VectorData *data,
144
144
{{c_type}} x) noexcept nogil:
145
145
146
- data.data[data.n ] = x
147
- data.n += 1
146
+ data.data[data.size ] = x
147
+ data.size += 1
148
148
149
149
{{endfor}}
150
150
@@ -164,7 +164,7 @@ ctypedef fused vector_data:
164
164
StringVectorData
165
165
166
166
cdef bint needs_resize(vector_data *data) noexcept nogil:
167
- return data.n == data.m
167
+ return data.size == data.capacity
168
168
169
169
# ----------------------------------------------------------------------
170
170
# Vector
@@ -209,26 +209,26 @@ cdef class {{name}}Vector(Vector):
209
209
{{endif}}
210
210
211
211
def __cinit__(self):
212
- self.data.n = 0
213
- self.data.m = _INIT_VEC_CAP
214
- self.ao = np.empty(self.data.m , dtype=np.{{dtype}})
212
+ self.data.size = 0
213
+ self.data.capacity = _INIT_VEC_CAP
214
+ self.ao = np.empty(self.data.capacity , dtype=np.{{dtype}})
215
215
self.data.data = <{{c_type}}*>self.ao.data
216
216
217
217
cdef resize(self):
218
- self.data.m = max(self.data.m * 4, _INIT_VEC_CAP)
219
- self.ao.resize(self.data.m , refcheck=False)
218
+ self.data.capacity = max(self.data.capacity * 4, _INIT_VEC_CAP)
219
+ self.ao.resize(self.data.capacity , refcheck=False)
220
220
self.data.data = <{{c_type}}*>self.ao.data
221
221
222
222
def __len__(self) -> int:
223
- return self.data.n
223
+ return self.data.size
224
224
225
225
cpdef ndarray to_array(self):
226
- if self.data.m != self.data.n :
226
+ if self.data.capacity != self.data.size :
227
227
if self.external_view_exists:
228
228
# should never happen
229
229
raise ValueError("should have raised on append()")
230
- self.ao.resize(self.data.n , refcheck=False)
231
- self.data.m = self.data.n
230
+ self.ao.resize(self.data.size , refcheck=False)
231
+ self.data.capacity = self.data.size
232
232
self.external_view_exists = True
233
233
return self.ao
234
234
@@ -254,45 +254,45 @@ cdef class StringVector(Vector):
254
254
StringVectorData data
255
255
256
256
def __cinit__(self):
257
- self.data.n = 0
258
- self.data.m = _INIT_VEC_CAP
259
- self.data.data = <char **>malloc(self.data.m * sizeof(char *))
257
+ self.data.size = 0
258
+ self.data.capacity = _INIT_VEC_CAP
259
+ self.data.data = <char **>malloc(self.data.capacity * sizeof(char *))
260
260
if self.data.data is NULL:
261
261
raise MemoryError()
262
262
263
263
cdef resize(self):
264
264
cdef:
265
265
char **orig_data
266
- Py_ssize_t i, m
266
+ Py_ssize_t i, orig_capacity
267
267
268
- m = self.data.m
269
- self.data.m = max(self.data.m * 4, _INIT_VEC_CAP)
268
+ orig_capacity = self.data.capacity
269
+ self.data.capacity = max(self.data.capacity * 4, _INIT_VEC_CAP)
270
270
271
271
orig_data = self.data.data
272
- self.data.data = <char **>malloc(self.data.m * sizeof(char *))
272
+ self.data.data = <char **>malloc(self.data.capacity * sizeof(char *))
273
273
if self.data.data is NULL:
274
274
raise MemoryError()
275
- for i in range(m ):
275
+ for i in range(orig_capacity ):
276
276
self.data.data[i] = orig_data[i]
277
277
278
278
def __dealloc__(self):
279
279
free(self.data.data)
280
280
281
281
def __len__(self) -> int:
282
- return self.data.n
282
+ return self.data.size
283
283
284
284
cpdef ndarray[object, ndim=1] to_array(self):
285
285
cdef:
286
286
ndarray ao
287
287
Py_ssize_t n
288
288
object val
289
289
290
- ao = np.empty(self.data.n , dtype=object)
291
- for i in range(self.data.n ):
290
+ ao = np.empty(self.data.size , dtype=object)
291
+ for i in range(self.data.size ):
292
292
val = self.data.data[i]
293
293
ao[i] = val
294
294
self.external_view_exists = True
295
- self.data.m = self.data.n
295
+ self.data.capacity = self.data.size
296
296
return ao
297
297
298
298
cdef void append(self, char *x) noexcept:
@@ -311,37 +311,37 @@ cdef class ObjectVector(Vector):
311
311
312
312
cdef:
313
313
PyObject **data
314
- Py_ssize_t n, m
314
+ Py_ssize_t size, capacity
315
315
ndarray ao
316
316
317
317
def __cinit__(self):
318
- self.n = 0
319
- self.m = _INIT_VEC_CAP
318
+ self.size = 0
319
+ self.capacity = _INIT_VEC_CAP
320
320
self.ao = np.empty(_INIT_VEC_CAP, dtype=object)
321
321
self.data = <PyObject**>self.ao.data
322
322
323
323
def __len__(self) -> int:
324
- return self.n
324
+ return self.size
325
325
326
326
cdef append(self, object obj):
327
- if self.n == self.m :
327
+ if self.size == self.capacity :
328
328
if self.external_view_exists:
329
329
raise ValueError("external reference but "
330
330
"Vector.resize() needed")
331
- self.m = max(self.m * 2, _INIT_VEC_CAP)
332
- self.ao.resize(self.m , refcheck=False)
331
+ self.capacity = max(self.capacity * 2, _INIT_VEC_CAP)
332
+ self.ao.resize(self.capacity , refcheck=False)
333
333
self.data = <PyObject**>self.ao.data
334
334
335
335
Py_INCREF(obj)
336
- self.data[self.n ] = <PyObject*>obj
337
- self.n += 1
336
+ self.data[self.size ] = <PyObject*>obj
337
+ self.size += 1
338
338
339
339
cpdef ndarray[object, ndim=1] to_array(self):
340
- if self.m != self.n :
340
+ if self.capacity != self.size :
341
341
if self.external_view_exists:
342
342
raise ValueError("should have raised on append()")
343
- self.ao.resize(self.n , refcheck=False)
344
- self.m = self.n
343
+ self.ao.resize(self.size , refcheck=False)
344
+ self.capacity = self.size
345
345
self.external_view_exists = True
346
346
return self.ao
347
347
0 commit comments