@@ -226,20 +226,22 @@ def __init__(self, dtype=np.int32, capacity=8, *, grow_use_add=None, grow_add=No
226
226
# self._zero = self._data[0]
227
227
228
228
@classmethod
229
- def from_values (cls , values , * , grow_use_add = None , grow_add = None ):
229
+ def from_values (cls , values , capacity = 8 , * , grow_use_add = None , grow_add = None ):
230
230
"""
231
231
Create a DynamicVector from an existing vector.
232
232
233
233
Parameters:
234
- values (sequence): The source array to initialize the vector.
234
+ values (sequence): The source array to initialize the vector.
235
+ capacity (int, optional): Initial minimum capacity of the underlying storage vector. Defaults to max(8, len(values)).
235
236
grow_use_add (int, optional): Custom threshold to switch from multiplicative to additive growth.
236
- grow_add (int, optional): Custom value for additive growth.
237
+ grow_add (int, optional): Custom value for additive growth.
237
238
238
239
Returns:
239
240
DynamicVector: A new dynamic vector initialized with the values from the input vector.
240
241
"""
241
242
try :
242
- capacity = len (values )
243
+ if len (values ) > capacity :
244
+ capacity = len (values )
243
245
except TypeError :
244
246
return cls .from_iter (values , grow_use_add , grow_add )
245
247
@@ -262,14 +264,15 @@ def from_values(cls, values, *, grow_use_add=None, grow_add=None):
262
264
return dyn
263
265
264
266
@classmethod
265
- def from_iter (cls , iterator , * , grow_use_add = None , grow_add = None ):
267
+ def from_iter (cls , iterator , capacity = 8 , * , grow_use_add = None , grow_add = None ):
266
268
"""
267
269
Create a DynamicVector from an iterator.
268
270
269
271
Parameters:
270
- iterator (iterator): The source iterator to initialize the vector.
272
+ iterator (iterator): The source iterator to initialize the vector.
273
+ capacity (int, optional): Initial minimum capacity of the underlying storage vector. Defaults to 8.
271
274
grow_use_add (int, optional): Custom threshold to switch from multiplicative to additive growth.
272
- grow_add (int, optional): Custom value for additive growth.
275
+ grow_add (int, optional): Custom value for additive growth.
273
276
274
277
Returns:
275
278
DynamicVector: A new dynamic vector initialized with the values from the input iterator.
@@ -294,7 +297,7 @@ def from_iter(cls, iterator, *, grow_use_add=None, grow_add=None):
294
297
if grow_add is None :
295
298
grow_add = iterator .grow_add
296
299
297
- dyn = cls (dtype , grow_use_add = grow_use_add , grow_add = grow_add )
300
+ dyn = cls (dtype , capacity , grow_use_add = grow_use_add , grow_add = grow_add )
298
301
dyn .append (value )
299
302
for value in iterator :
300
303
dyn .append (value )
0 commit comments